爲什麼我可以在不使用全局變量的情況下在以下函數中使用類的實例?函數可以在不使用python全局的情況下訪問類的實例嗎?
class TestClass:
def __init__(self, name):
self.name = name
def MyName(self):
print "My name is: " + self.name
def testFunc():
# why can I use the instance here?
print "in testFunc()"
instance.MyName()
if __name__ == '__main__':
instance = TestClass("abc")
print "in __main__"
instance.MyName()
testFunc()
if __name__ =='__main__':仍然是全局的 – 2014-11-05 10:03:04