closures and dynamic binding

Aaron "Castironpi" Brady castironpi at gmail.com
Wed Oct 1 14:08:28 EDT 2008


On Oct 1, 5:43 am, jhermann <Juergen.Herm... at 1und1.de> wrote:
> I didn't see this mentioned in the thread yet: the double-lambda is
> unnecessary (and a hack). What you should do when you need early
> binding is... early binding. ;)
>
> Namely:
>
> f = [lambda n=n: n for n in range(10)]
> print f[0]()
> print f[1]()
>
> Note the "n=n", this prints 0 and 1 instead of 9/9.

Yes it was mentioned earlier.  I think its similar.  They both create
ten new namespaces.  You could do something like this (I hit a bump
with eval and globals() when I tried it):

def early( string ):
   return eval( string, current_namespace )

f = [early( 'lambda: n' ) for n for n in range(10)]
print f[0]()
print f[1]()

Furthermore, I don't think the binding semantics of the language are
completely static binding.  What does that definition say about
mutating a value?  I think it's ambiguous and there's no obvious use
case that favors either one.



More information about the Python-list mailing list