2013/7/2 Andrea Griffini <agriff at tin.it> > I'm trying to understand how CPython implements closure variable capture > and there is one minor point I can't understand. > > When a local is captured it gets allocated in co_cellvars and is accessed > with (LOAD|STORE)_DEREF, and this is clear. > However when a local is coming from a parameter it gets ALSO allocated in > co_varnames even if the local slot apparently is not accesible because > *_FAST opcodes are not generated. > > Is there a technical reason for this? It happens in CPython 2, 3 and even > in PyPy... > > co_varnames is also used in error messages, for example in the following code: >>> def f(): ... def g(): ... x ... print x ... x = 1 ... return g ... >>> f() UnboundLocalError: local variable 'x' referenced before assignment This is also needed when x is a parameter of f(), for inspect.signature of course, but also because in python3 you can "del x". -- Amaury Forgeot d'Arc -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20130702/8f93fe71/attachment.html>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4