[docs] [issue28853] locals() and free variables

Marco Buttu report at bugs.python.org
Thu Dec 1 11:54:37 EST 2016


New submission from Marco Buttu:

The locals() documentation [1] says that "Free variables are returned by locals() when it is called in function blocks". A free variable inside a function has a global scope, and in fact it is not returned by locals()::

>>> x = 33
>>> def foo():
...     print(x)
...     print(locals())
... 
>>> foo()
33
{}

Maybe "function blocks" here means "closure"? Does the doc mean this?

>>> def foo():
...     x = 33
...     def moo():
...         print(x)
...         print(locals())
...     return moo
... 
>>> moo = foo()
>>> moo()
33
{'x': 33}

In that case, I think it is better to write "closures" instead of 
"function blocks".


[1] https://docs.python.org/3/library/functions.html#locals

----------
assignee: docs at python
components: Documentation
messages: 282200
nosy: docs at python, marco.buttu
priority: normal
severity: normal
status: open
title: locals() and free variables
type: enhancement
versions: Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28853>
_______________________________________


More information about the docs mailing list