class variable won't icrement!

Aahz aahz at pythoncraft.com
Thu Sep 12 12:20:11 EDT 2002


In article <PQ2g9.191834$l_4.58187 at atlpnn01.usenetserver.com>,
Steve Holden <sholden at holdenweb.com> wrote:
>"Aahz" <aahz at pythoncraft.com> wrote in message
>news:alq9hf$bn$1 at panix1.panix.com...
>> In article <alpner$bm$1 at wanadoo.fr>,
>> Eric Brunel  <eric.brunel at pragmadev.com> wrote:
>>>
>>>I always found that inheritance of class attributes to instance attributes
>>>was really misleading, and I even think it would be better if it were
>>>simply removed from Python. IMHO, it would far more conform to the
>>>"explicit is better than implicit" rule that is one of the basis of Python.
>>
>> If I think of it as "scope resolution" rather than "inheritance", I find
>> that it makes more sense.  It works exactly the same way as hiding a
>> global with a local.
>
>Well, not quite exactly the same way. If a local hides a global
>then there's no way to access the global without explicit use of a
>namespace.

Exactly!  The only difference is that it's an error in a function to
attempt to read a global and then later modify it (in the absence of the
global statement).

>A class variable is available by instance-qualified name up until the time
>the instance variable with the same name is created, which is why statements
>like
>
>    self.var += 1
>
>can be so confusing. Especially when you compare operations that give
>different results for mutable and immutable objects.

It might make sense to tighten the rules for modifying class variables,
just the way this gives an error:

x = 1
def foo():
    print x
    x = x + 1

The mutable/immutable thing is exactly the same for functions and
instances.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/



More information about the Python-list mailing list