Re: Class Variable Access and Assignment



Op 2005-11-03, Stefan Arentz schreef <stefan.arentz@xxxxxxxxx>:
> Antoon Pardon <apardon@xxxxxxxxxxxxxxx> writes:
>
>> Op 2005-11-03, venk schreef <venkatasubramanian@xxxxxxxxx>:
>> > You see,
>> > The seen behavior is due to the result of python's name
>> > binding,scoping scheme.
>>
>> I know what causes the behaviour. But I still think it is
>> not sane behaviour.
>>
>>
>> > ...
>> >
>> > the same thing happens in the case of b.a = b.a + 2 .... search for b.a
>> > not found, read the value from the enclosing scope (of the class
>> > object).... then assign b.a to the local scope, with the value 3.
>>
>> This is an explanation depending on a specific implementation.
>>
>> Now can you give me a language design argument that supports the
>> idea that in "b.a = b.a + 2" b.a refers to two different objects.
>>
>> And even if you could do that, can you give such an argument that
>> in "b.a += 2" that one occurence of b.a should refer to two different
>> objects.
>
> Your problem is a namespace conflict together with a certain
> stubborness about lookup order :-)
>
> It is really simple. When you say b.a then the instance variable 'a'
> is looked up first. If it does not exist then a class variable lookup
> is done.

Fine, we have the code:

b.a += 2

We found the class variable, because there is no instance variable,
then why is the class variable not incremented by two now?

> Remember, Python is a dynamic language.

So? Python being a dynamic language doesn't prevent the following to fail:

>>> a=1
>>> def f():
.... a += 2
....
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
UnboundLocalError: local variable 'a' referenced before assignment

> It is all according to how things have been in Python for a long time.

Unsane behaviour for a long time is still unsane behaviour.

> The real issue here is that you should propery name class variables so
> that there can't be any confusion about class or instance scope. I use
> all uppercase identifiers for class variables for example.

The fact that this can be regarded as unwise coding, doesn't imply
it is sane behaviour of python. Variable shadowing happens. I don't
consider it sane behaviour if the same reference in a line gets
resolved in different name spaces

--
Antoon Pardon
.



Relevant Pages

  • Re: Good Form
    ... am used to class variables ... 2/ Since Python is 100% object, functions are objects too ... you can think of direct attribute acces as a computed attribute ... Languages like C++ or Java not having support for computed ...
    (comp.lang.python)
  • Re: using "private" parameters as static storage?
    ... such information is either class variables or instance variables. ... any time Python lacks a feature, the Python community's party line is "You don't need that feature!" ... I understand embracing the language rather than fighting against it, but that can be taken too far -- if somebody expresses a need, and is earnestly asking for input on a real programming problem, I'd think the nice thing would be to explore the programming problem with them, rather than arguing with them that they don't need what they claim they need. ...
    (comp.lang.python)
  • Re: Declaring a class level nested class?
    ... I keep getting nestedClass isn't defined. ... You'll get an error about Python being unable to find the class variable:: ... To refer to class variables, ... def method1: ...
    (comp.lang.python)
  • Re: using "private" parameters as static storage?
    ... such information is either class variables or instance variables. ... the Python community's party line is "You don't need that feature!" ... the nice thing would be to explore the programming problem with them, ...
    (comp.lang.python)
  • Good Form
    ... I am new to Python but come from a C++ background so I am trying to ... connect the dots:). ... I am really liking what I see so far but have ... am used to class variables being accessable only through methods ...
    (comp.lang.python)