Closure/binding problem or misunderstanding

Bob.Sidebotham at gmail.com Bob.Sidebotham at gmail.com
Fri Nov 9 10:03:30 EST 2007


On Nov 9, 9:49 am, Paul Hankin <paul.han... at gmail.com> wrote:

> It's behaving as defined though, and the usual work-around is to add a
> variable with a default value.
>
> class path(object):
>     def __init__(self, **subdirs):
>         for name, path in subdirs.iteritems():
>             def getpath(path=path):
>                 return path
>             setattr(self, name, getpath)
>

Thanks, Paul. That's helpful. I will re-read the reference manual, and
see if I can find out where this behavior is defined. It looks like
it's binding both locals and globals, but not actually taking a
snapshot in time, as would, say Perl (I think). From an efficiency POV
this makes great sense, and I can see that you get everything you need
by essentially creating the closure yourself (by putting everything in
the local space for the function).

Bob




More information about the Python-list mailing list