python

时间:2017-01-04 17:23:04

标签: python-2.7 dictionary global-variables

我对具有全局功能的词典的行为感到困惑。在下面的例子中,为什么第一个案例打印{' abc':}}而第二个案例打印{' abc':456}?我正在使用python 2.7。

第一个例子:

def foo():
  test_dict = {}
  bar()

def bar():
    test_dict['abc'] = 123


test_dict = {'abc':456}
foo()
print test_dict

第二个例子:

test_dict = {'abc':456}

def foo():
  test_dict = {}
  test_dict['abc'] = 123
foo()
print test_dict

0 个答案:

没有答案