Python的命名空间的概念跟Java、Objective-C的命名空间的概念查不到,下面是一个例子,你们慢慢体会。大概意思是,同一名称的,就近原则优先,里层的可以使用外层的,外层的不能使用里层的。
>>> def outer_foo():
... a = 10
... def inner_foo():
... a = 20
... print "inner_foo,a = ",a
... inner_foo()
... print "out_foo,a = ",a
...
>>> a = 30
>>> outer_foo()
inner_foo,a = 20
out_foo,a = 10
>>> print "a = ",a
a = 30
网友评论