[Python-Dev] terminology for "free variables" in Python

Guido van Rossum guido at python.org
Fri Sep 10 17:17:31 CEST 2010


On Fri, Sep 10, 2010 at 12:00 AM, Eli Bendersky <eliben at gmail.com> wrote:
>>> def some_func(myparam):
>>
>> >     def internalfunc():
>> >         return cc * myparam
>> >
>> > CPython infers that in 'internalfunc', while 'myparam' is free, 'cc' is
>>
>> What exactly do you mean by "infers" ? How do you know that it infers
>> that? How does it matter for your understanding of the code?
>
> The easiest way I found to see what CPython thinks is use the 'symtable'
> module. With its help, it's clear that in the function above, myparam is
> considered free while cc is considered global. When querying symtable about
> the symbol myparam, the is_free method returns True while the is_global
> method returns False, and vice versa for cc.
>
> Of course it can also be seen in the code of symtable.c in function
> analyze_name, and as Nick showed in his message it also affects the way
> bytecode is generated for the two symbols.
>
> My intention in this post was to clarify whether I'm misunderstanding
> something or the term 'free' is indeed used for different things in
> different places. If this is the latter, IMHO it's an inconsistency, even if
> a small one. When I read the code I saw  'free' I went to the docs only to
> read that 'free' is something else. This was somewhat confusing.

I'm still not clear if my explanation that globals are a subset of
free variables got rid of the confusion. The full name for what
CPython marks as "free" would be "free but not global" but that's too
much of a mouthful.

Also you're digging awfully deep into the implementation here -- AFAIC
CPython could have called them "type A" and "type B" and there would
not have been any problem for compliance with the langage reference.

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list