Class Variable Access and Assignment

Antoon Pardon apardon at forel.vub.ac.be
Fri Nov 4 05:54:18 EST 2005


Op 2005-11-04, Stefan Arentz schreef <stefan.arentz at gmail.com>:
> 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.

It has nothing to do with global variables, the same thing happens
with nested scopes.

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

  g()

f()



More information about the Python-list mailing list