標題可能是誤導或不準確的,所以如果我錯了,請糾正我。跨模塊的Python變量範圍
我有一個包的結構是這樣的:
common
__init__.py
foo.py
這裏是代碼:
common/__init__.py
name = 'name_common'
def print_current_file_name():
print('current file name: ' + __file__)
def print_name():
print('name is: ' + eval('name'))
foo.py
from common import print_current_file_name, print_name
name = 'name_foo'
if __name__ == '__main__':
print_current_file_name()
print_name()
如果我這樣做:
>>> python foo.py
我會得到這樣的:
current file name: /tmp/common/__init__.py
name is: name_common
但我期望的結果是:
current file name: /tmp/common/foo.py
name is: name_foo
我錯過了什麼?我怎樣才能做到這一點? 我甚至不知道哪些關鍵字應該google ...
eval
的使用很奇怪,但這些代碼僅用於演示目的。
我同意,但我只是想知道是否有任何的方式來實現這一目標(不使用'class') – amigcamel
@amigcamel你描述動態範圍的語言(突出的例子是Scheme和shell); Python是靜態範圍的。 – chepner