经过25年的Python编程,我感到困惑。为什么这种装置(挤压到最低限度(打印"无"?我希望" thing.implementation1.juggernaut对象在0xblahblahblah"。这里发生了什么?
main.py
import thing
thing.run()
thing/__ init __。py
from .implementation1 import init
from .implementation1.main import *
thing/inashation1/__ init __。py
foo = None
class Juggernaut:
pass
def init():
global foo
foo = Juggernaut()
thick/inashation1/main.py
from . import init, foo
def run():
init()
print(repr(foo))
我找到了一个实用的解决方案,但仍然不明白它为什么起作用。将" INIT"功能从thing/implementation1/__init__.py
移动到thing/implementation1/main.py
解决了问题。
thing/__ init __。py
from .implementation1.main import *
thing/inashation1/__ init __。py
foo = None
class Juggernaut:
pass
thick/inashation1/main.py
from . import Juggernaut
def init():
global foo
foo = Juggernaut()
def run():
init()
print(repr(foo))