Over my head with descriptors

Christian Kastner usenet at kvr.at
Sat Dec 16 16:02:02 EST 2006


Tim Roberts wrote:
> "Sarcastic Zombie" <sarcasticzombie at gmail.com> wrote:
>> Code included below.
>>
>> Basically, I've created a series of "question" descriptors, which each
>> hold a managed value. This is so I can implement validation, and render
>> each field into html automatically for forms.
>>
>> My problem is this: every instance of my "wizard" class has unique self
>> values, but they share the exact same descriptor values.
>>
>> ...
>> class Test(Wizard):
>> 	grouping = [
>> 		[ 'age', 'weight' ],
>> 		[ 'feet', 'inches' ],
>> 		['name', 'cash', 'fav_color', 'happy', 'birthday'] ]
>>
>> 	def __new__(self):
>> 		age = Q_Integer("Your Age:", "age", 99)
>> 		weight = Q_Integer("Your Weight:", "weight", 200)
>> 		feet = Q_Integer("Feet tall:", "feet", 6)
>> 		inches = Q_Integer("Inches Tall:", "inches", 0)
>> 		name = Q_Chars("Your Name:", "name", max_length=15, required=True)
>> 		cash = Q_Float("Money in hand?", "cash", required=True,
>> default=55.50)
>> 		fav_color = Q_Chars("Your favorite color?", "fav_color",
>> required=True, max_length=50, choices=C_CHOICES)
>> 		homezip = Q_Zip("Your zip code?", "homezip", required=True, )
>> 		happy = Q_Bool("Are you happy?", "happy", default=False)
>> 		birthday = Q_Date("Your Birthday:", "birthday")
> 
> The __new__ method is called with the CLASS as its first argument, not the
> new instance.  __new__ is supposed to RETURN the new instance.  So, when
> you set "age", you are setting a CLASS attribute that will be shared by all
> instances.

As long as "age" really is set on the class. In the code above, "age" is
just a local variable.

> Is there a reason you don't just use __init__ instead of __new__, and use
> "self.age" and "self.weight" and so on?

I was asking myself the same thing...

Chris



More information about the Python-list mailing list