creating many similar properties

Carl Banks pavlovevidence at gmail.com
Wed Oct 18 04:37:08 EDT 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)
        printvars()

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.


Carl




More information about the Python-list mailing list