Class Variable Access and Assignment

Magnus Lycka lycka at
Fri Nov 4 16:52:59 CET 2005

Antoon Pardon wrote:
  > I have looked and didn't find it in the language reference.
> This is what I have found:
> An augmented assignment expression like x += 1 can be rewritten
> as x = x + 1 to achieve a similar, but not exactly equal effect.

It's just a little further down. I'll post the quote once more (but
this is the last time ;^):

"""For targets which are attribute references, the initial value is 
retrieved with a getattr() and the result is assigned with a setattr(). 
Notice that the two methods do not necessarily refer to the same 
variable. When getattr() refers to a class variable, setattr() still 
writes to an instance variable. For example:

class A:
     x = 3    # class variable
a = A()
a.x += 1     # writes a.x as 4 leaving A.x as 3"""

I'd say it's documented...

> That doesn't change the fact that the current behaviour is
> on occasions awkward or whatever you want to call it.

I fear that this has to do with the way reality works. Perhaps
it's due to Gödel's incompleteness theorems... :)

Sure, Python has evolved and grown for about 15 years, and
backward compatibility has always been an issue, but the
management and development of Python is dynamic and fairly
open-minded. If there had been an obvious way to change this
in a way that solved more problems than it caused, I suspect
that change would have happened already.

> I also find that people underestimate the magic that
> is going on in python. But just because you are familiar with
> the magic, doesn't make it less magic. IMO python shows its
> history a little. 

If Guido would design a new language today, that was aiming at
the kind of tasks Python solves, I'm sure it wouldn't be identical
to the current Python. The languages I'm most experienced in besides
Python are C++ and SQL. Compared to those beasts, Python is a wonder
in clarity and consistency. I don't think a single vendor has managed
to fully implement the SQL standard, and it's known that the standard
contains bugs, inconsitencies and gaps, even though (or because) it's
been worked on for more than 20 years. The C++ spec is marginally
better. Of course, there isn't a formal specification for Python. I
don't know if the language reference is so complete that someone
could actually write another really compatible Python implementation
based on just the reference manual. Still the difference is drastic.
I suspect that only few people in the SQL standard committee fully
understand the spec (maybe C.J. Date and Hugh Darwen does) and all
the things happening under the hood in C++ is staggering, considering
that this language is really just a fancy assmbler that can't even
manage memory for the programmer!

> I'm after nothing particular. The only thing I'm frustrated about
> is the way in which some people seem willing to defend python
> just because it is python. If the only reaction I would have
> gotten would have been something like: Yeah that seems a bit
> awkward but fixing this would break more than it would cure,
> I would have left it as it is.

That's probably what most people think, but we're not entirely
rational. We're human. An emotional posting will probably attract
equally emotional responses.

> I have rarely indicated I wanted things to be fixed. Sure I would
> like it if some things were different, but I recognize that there
> are more important things that needs to be resolved.
> Does that mean I shouldn't mention things that IMO could have been
> better or that I should only mention them in the softest of
> language that certainly can't be interpreted as emotional language.

Personally, I want comp.lang.python to work as a way for me to learn
new things about Python, to get help if I'm stuck with something, and
as a way for me to inform others about Python stuff. It's also a part
of the Python community--an arena where I communicate with other
Pythonistas. This is something important for me, both professionally
and personally. I try to think an extra time before I post messages.
It this message meaningful? Does it add anything of value? Am I just
repeating what is already said? Is this message likely to have some
kind of positive impact or will it just be ignored? Might I hurt
someone? Am I building useful relationships?

When it works as it should, people you've never met before will buy
you beer or lunch when you happen to be in their neighbourhood. At
least they might come up to you and chat if you go to a Python
conference. They might also offer you jobs or contracts etc. This
is really nice and valuable. Something to handle with care.

More information about the Python-list mailing list