python sub_function_scope_test.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python sub_function_scope_test.py相关的知识,希望对你有一定的参考价值。

>>> def foo():
...     a = 1
...     def foo_a():
...         a = 2
...         print 'foo_a a:', a
...     foo_a()
...     print 'foo a:', a
...
>>> foo()
foo_a a: 2
foo a: 1


>>> def foo():
...     class A: pass
...     a = A()
...     b = 1
...     def foo_a():
...         a.c = b
...     foo_a()
...     print a.__dict__
...     print a.c
...
>>> foo()
{'c': 1}
1


>> n = 1
>>> def foo():
...     print n
...
>>> foo()
1


>>> n = 1
>>> def foo():
...     n + 1
...
>>> foo()


>>> n = 1
>>> def foo():
...     n = n + 1
...
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'n' referenced before assignment

以上是关于python sub_function_scope_test.py的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识