Class Variable Access and Assignment

Bengt Richter bokr at oz.net
Fri Nov 4 20:43:29 EST 2005


On 04 Nov 2005 11:04:58 +0100, Stefan Arentz <stefan.arentz at gmail.com> wrote:

>Antoon Pardon <apardon at forel.vub.ac.be> writes:
>
>> 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()
>
>No that has nothing to do with resolving things at runtime. Your example
>does not work because the language is very specific about looking up
>global variables. Your programming error, not Python's shortcoming.
>
If someone has an old version of Python handy, I suspect that it used
to "work", and the "a" on the right hand side was the global "a" because
a local "a" hadn't been defined until the assignment, which worked to
produce a local binding of "a". Personally, I like that better than
the current way, because it follows the order of accesses implied
by the precedences in expression evaluation and statement execution.
But maybe I don't RC ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list