<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2013/7/2 Andrea Griffini <span dir="ltr"><<a href="mailto:agriff@tin.it" target="_blank">agriff@tin.it</a>></span><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div dir="ltr">I'm trying to understand how CPython implements closure variable capture and there is one minor point I can't understand.<div><br></div><div>When a local is captured it gets allocated in co_cellvars and is accessed with (LOAD|STORE)_DEREF, and this is clear.</div>
<div>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.</div><div><br></div><div>
Is there a technical reason for this? It happens in CPython 2, 3 and even in PyPy...</div><span class=""><font color="#888888"><div><br></div></font></span></div></blockquote><div> </div><div>co_varnames is also used in error messages, for example in the following code:</div>
<div><br></div><div>>>> def f():</div><div>... def g():</div><div>... x</div><div>... print x</div><div>... x = 1</div><div>... return g</div><div>... </div><div>>>> f()</div><div>
UnboundLocalError: local variable 'x' referenced before assignment<br></div><div><br></div><div>This is also needed when x is a parameter of f(), for inspect.signature of course,</div><div>but also because in python3 you can "del x".</div>
<div><br></div><div>-- <br></div></div>Amaury Forgeot d'Arc
</div></div>