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

Guido van Rossum guido at python.org
Thu Sep 9 18:55:23 CEST 2010


On Thu, Sep 9, 2010 at 9:43 AM, Eli Bendersky <eliben at gmail.com> wrote:
> The execution model section of the Python reference manual defines free
> variables as follows:
>
>     "If a variable is used in a code block but not defined there, it is
> a free variable"
>
> This makes sense and fits the academic definition. The documentation of the
> symtable module supports this definition - it says about is_free(): "return
> True if the symbol is referenced in its block but not assigned to".
>
> However, it appears that in the CPython front-end source code (in particular
> the parts dealing with the symbol table), a free variables has a somewhat
> stricter meaning. For example, in this chunk of code:
>
> 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?

> global because 'cc' isn't bound in the enclosing scope, although according
> to the definitions stated above, both should be considered free. The
> bytecode generated for loading cc and myparam is different, of course.
>
> Is there a (however slight) inconsistency of terms here, or is it my
> misunderstanding?

That remains to be seen (please answer the questions above for a
better understanding of your question).

Maybe this helps though: global variables are a subset of free
variables, and they are treated different for various reasons (some
historic, some having to do with optimizations in the code -- I think
you saw the latter in the bytecode).

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


More information about the Python-Dev mailing list