creating many similar properties

Carl Banks pavlovevidence at
Wed Oct 18 10:37:08 CEST 2006

George Sakkis wrote:
> There's a subtle common bug here: all _get and _set closures will refer
> to the last property only. You have to remember to write "def
> _set(self,v,prop=prop)" and similarly for _get to do the right thing.

Sorry.  My mistake.

> By the way, I can't think of a case where the current behavior (i.e.
> binding the last value only) is the desired one. Is this just an
> implementation wart or am I missing something ?

def some_function(a):
    def printvars():
        print "DEBUG: %r,%r,%r" % (a,b,i)
    for i in some_range():
        b = something(i)

If you fix the value of the closure at function definition time,
printvars() above doesn't work.  One way or another, someone's going to
get surprised.  Better to let it be the experts.


More information about the Python-list mailing list