Class Variable Access and Assignment

Antoon Pardon apardon at forel.vub.ac.be
Fri Nov 4 04:26:07 EST 2005


Op 2005-11-03, Mike Meyer schreef <mwm at mired.org>:
> Antoon Pardon <apardon at forel.vub.ac.be> writes:
>>> What would you expect to get if you wrote b.a = b.a + 2?
>> I would expect a result consistent with the fact that both times
>> b.a would refer to the same object.
>
> Except they *don't*. This happens in any language that resolves
> references at run time.

Python doesn't resolve references at run time. If it did the following
should work.

a = 1
def f():
  a = a + 1

f()

But letting that aside. There is still a difference between resolving
reference at run time and having the same reference resolved twice
with each resolution a different result.

> Changing that would be changing a fundamental
> - and *important* - feature of Python. Arbitrary restrictions to
> prevent a single case of this from doing something people who aren't
> used to suvh behavior are kludges, and would constitute a wart on the
> language, pure and simple.

Python already has its warts. If you want to argue that fixing this
would make a bigger wart then the wart it is now. Fine I will accept
that.

> If you think this is bad, you should consider Jensen's device. It uses
> call-by-name, which Python doesn't have.

Actually, I would have thought it very interesting should python 
have provided some choice in parameter semantics.


>> I think it even less sane, if the same occurce of b.a refers to two
>> different objects, like in b.a += 2
>
> That's a wart in +=, nothing less. The fix to that is to remove +=
> from the language, but it's a bit late for that.
>
>      <mike

Well we agree that there is a wart somewhere.

-- 
Antoon Pardon



More information about the Python-list mailing list