property problems

newt_e at blueyonder.co.uk newt_e at blueyonder.co.uk
Thu Nov 14 02:50:00 EST 2002


Mike wrote :

>Okay, there's a number of things going on here:
>
>    * properties only work (properly) with new-style classes, new-style
>     classes derive from other new-style classes, normally "object".
>      Your class GlobalVars is an old-style class.  Derive from object
>     to prevent problems which _will_ come up later on.

What defines my class as an old-style class? How would I turn it into a new class?

>   * you are accessing a property which stores a list as if it was the
>     setter method of that property.  You want to do:
>
>        gv.stats =
>        [['Height',self.height],['Build',self.build],['Strength',str]]
>

Doh!. I used to gv.set_stats([['Height',self.height],['Build',self.build],['Strength',str]]) until
I saw something about the property statement, and forgot to change the call line.

>    * gv.stats( ... ) is actually accessing the property value, which is
>      currently a list object, and then trying to call it as if it were
>      a function/method/callable object (it isn't one).

Took a while, but I understand what you're saying now.

>
>HTH,
>Mike

>> I wrote:
>
>> Hi,
>>  
>> I've got a problem trying to use a property in a class module. The 
>> class module is listed below (it's called globals.py):
>>  
>> class GlobalVars:
>>  
>>     def __init__(self):
>>         self.l_stats = []
>>         return
>>  
>>     def set_stats(self,val):
>>         self.l_stats = val[:]
>>         return
>>  
>>     def get_stats(self):
>>         return self.l_stats
>>  
>>     stats = property(get_stats, set_stats)
>> gv = GlobalVars()
>>  
>> I've got a further module which does the following:
>>  
>> from globals import gv
>> gv.stats([['Height',self.height],['Build',self.build],['Strength',str]])

[ error snipped ]

Thanks Mike,

Newt




More information about the Python-list mailing list