Class Variable Access and Assignment
Bengt Richter
bokr at oz.net
Fri Nov 4 22:10:41 EST 2005
On Fri, 04 Nov 2005 09:24:41 -0500, Christopher Subich <csubich.spam.block at spam.subich.block.com> wrote:
>Steven D'Aprano wrote:
>> On Thu, 03 Nov 2005 14:13:13 +0000, Antoon Pardon wrote:
>>
>>
>>>Fine, we have the code:
>>>
>>> b.a += 2
>>>
>>>We found the class variable, because there is no instance variable,
>>>then why is the class variable not incremented by two now?
>>
>>
>> Because b.a += 2 expands to b.a = b.a + 2. Why would you want b.a =
>> <something> to correspond to b.__class__.a = <something>?
>
>Small correction, it expands to b.a = B.a.__class__.__iadd__(b.a,2),
>assuming all relevant quantities are defined. For integers, you're
>perfectly right.
But before you get to that, a (possibly inherited) type(b).a better
not have a __get__ method trumping __class__ and the rest ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list