현재 PyGame을 사용하여 다중 파일 파이썬 (2.6.5) 게임을 작성하려고합니다. 문제는 "pyconsole.py"파일 중 하나가 기본 파일 "main.py"에 의해 가져온 다른 객체의 인스턴스에서 메소드를 호출 할 수 있어야한다는 것입니다. 문제는 모든 게임 개체 (플레이어의 배, 적의 배, 방송국 등)의 인스턴스를 보유 할 기본 파일의 목록을 가지고 있지만 그 안에있는 목록에서 메서드를 호출 할 수없는 것처럼 보입니다. 메인 루프가 시작되기 전에 "main.py"에서 from pyconsole import *
을 수행하고 있음에도 불구하고 "pyconsole.py". 이것은 단순히 가능하지 않으며, 대신 M4를 사용하여 모든 파일을 하나의 단일 파일로 결합한 다음 바이트 코드 컴파일 및 테스트/배포해야합니까?파이썬 액세스 부모 객체 인스턴스
예 :
파이썬 이름, 클래스, 모든 물건의 내 다소 제한 이해 여기에 올바른 경우bash$ cat test.py
#!/usr/bin/python
import math, distancefrom00
foo = 5
class BarClass:
def __init__(self):
self.baz = 10
def get(self):
print "The BAZ is ", self.baz
def switch(self)
self.baz = 15
self.get()
bar = BarClass()
def main():
bar.switch()
print distancefrom00.calculate([2, 4])
if __name__ == '__main__': main()
bash$ cat distancefrom00.py
#!/usr/bin/python
import math
import test
def calculate(otherpoint):
return str(math.hypot(otherpoint[0], otherpoint[1]))+" (foo = "+str(test.foo)+"; "+test.bar.get()+")"
bash$ python test.py
The BAZ is 15
The BAZ is 10
Traceback (most recent call last):
File "test.py", line 24, in <module>
if __name__ == '__main__': main()
File "test.py", line 22, in main
print distancefrom00.calculate([2, 4])
File "/home/archie/Development/Python/Import Test/distancefrom00.py", line 8, in calculate
return str(math.hypot(otherpoint[0], otherpoint[1]))+" (foo = "+str(test.foo)+"; "+test.bar.get()+")"
TypeError: cannot concatenate 'str' and 'NoneType' objects
의 NoneType 이름 test.bar.get()
것을 의미한다 - 따라서는, test.bar
- 아무것도에 할당되지 않은 .
"방법을 호출 할 수없는 것 같습니다"라는 의미는 무엇입니까? 몇 가지 샘플 코드를 포함하고 예외를 제공하십시오. – viraptor
아 - 그리고를 위해서. 자신의 안전을 위해 빌드 스크립트에 M4를 포함시키지 마십시오. 파이썬 모듈이 올바르게 작동하면 어딘가에 버그가 있습니다. M4는 악합니다. – viraptor
호기심에서 벗어나 M4 ('-P' 플래그 사용을 잊어 버려서 기존 텍스트를 망가 뜨리지 않는다)는 어떨까요? – mmirate