Class Variable Access and Assignment
Antoon Pardon
apardon at forel.vub.ac.be
Fri Nov 4 02:46:45 EST 2005
Op 2005-11-03, Steven D'Aprano schreef <steve at REMOVETHIScyber.com.au>:
> 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>?
That is an implemantation detail. The only answer that you are given
means nothing more than: because it is implemented that way.
> I'm not saying that it couldn't, if that was the model for inheritance you
> decided to use. I'm asking why would you want it? What is your usage case
> that demonstrates that your preferred inheritance model is useful?
It has nothing to do with a model for inheritance, but with a model of
name resolution.
The hierarchie of searching an instance first in an object and then in
a class isn't that different from searching first in a local namespace
and then in a more global namespace.
When we search names in a function we don't resolve the same name in
different name spacese each occurence of the same name in the same
function occurs in the same namespace.
But with class variables we can have that one and the same name
on a line refers to two different namespaces at the same time.
That is IMO madness. You may argue that the madness is of little
importance, you can argue that because of the current implementation
little can be done about it. But I don't see how one can defend
it as sane behaviour.
--
Antoon Pardon
More information about the Python-list
mailing list