[Python-Dev] nested scopes confusion

M.-A. Lemburg mal@lemburg.com
Wed, 05 Dec 2001 09:28:18 +0100


Thomas Heller wrote:
> 
> From: "Thomas Heller" <thomas.heller@ion-tof.com>
> > How do I achieve the desired effect? Note that the default argument
> > trick (def mth(i=i): ...) does not work because *args is present.
> Answering my own question (sorry), this works:
> 
> def functions():
>     result = []
>     for i in range(10):
>         def make_function(index):
>             def mth():
>                 return index
>             return mth
>         mth = make_function(i)
>         result.append(mth)
>     i = 25
>     return result
> 
> for mth in functions():
>     print mth()
> 
> But how will I understand this 3 months from now?

Why do you put the function definitions *inside*
the functions() constructor ? AFAICTL the confusion is caused
by this overly complicated nesting which doesn't seem
necessary.

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/