我試圖創建一個簡單的多級封裝:NameError定義蟒蛇多級封裝
test_levels.py
level1/
__init__.py (empty file)
level2/
__init__.py (only contents: __all__ = ["leaf"])
leaf.py
leaf.py:
class Leaf(object):
print("read Leaf class")
pass
if __name__ == "__main__":
x = Leaf()
print("done")
test_levels.py:
from level1.level2 import *
x = Leaf()
運行leaf.py直接正常工作,但運行test_levels.py返回下面的輸出, 其中我是exp沒有輸出:
read Leaf class
Traceback (most recent call last):
File "C:\Dev\intranet\test_levels.py", line 2, in <module>
x = Leaf()
NameError: name 'Leaf' is not defined
有人能指出我做錯了什麼嗎?
我想`從.leaf`只會工作在Python 3中(或者如果你在Python 2中從`__future__`導入absolute_imports)。 – 2010-11-26 16:51:47
是的,但我認爲topicstarter完全使用python3,因爲他寫了print(...)而不是print ... – werehuman 2010-11-26 16:59:14