Python 2.7を使用しています。グローバル変数counter
をtestlib.py
からuselib.py
にインポートしようとしましたが、返される結果は0
プログラムのuselib.py
にあります。なぜ2
は返されませんか?ありがとう。Python 2.7グローバル変数の問題
testlib.py、
counter = 0
class Foo:
def __init__(self):
pass
def Count(self):
global counter
counter += 1
uselib.py、Pythonのドキュメント毎
from testlib import Foo
from testlib import counter
f = Foo()
f.Count()
g = Foo()
g.Count()
print counter # output is 0 other than 2
私はそれが現在のスコープにコピーをインポート推測しているので、それが正しい 'counter' – Li357
@AndrewLを修正しない、あなたは何をすべきか。コピーをインポートする意味ですか? –
Related Post http://stackoverflow.com/questions/15959534/python-visibility-of-global-variables-in-imported-modules –