Class Variable Access and Assignment

Antoon Pardon apardon at forel.vub.ac.be
Mon Nov 7 09:19:46 CET 2005


Op 2005-11-04, Steven D'Aprano schreef <steve at REMOVETHIScyber.com.au>:
> On Fri, 04 Nov 2005 09:03:56 +0000, Antoon Pardon wrote:
>
>> Op 2005-11-03, Steven D'Aprano schreef <steve at REMOVETHIScyber.com.au>:
>>> On Thu, 03 Nov 2005 13:01:40 +0000, Antoon Pardon wrote:
>>>
>>>>> Seems perfectly sane to me. 
>>>>>
>>>>> 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.
>>>
>>> class RedList(list):
>>>     colour = "red"
>>>
>>> L = RedList(())
>>>
>>> What behaviour would you expect from len(L), given that L doesn't have a
>>> __len__ attribute?
>> 
>> Since AFAICT there is no single reference to the __len__ attribute that
>> will be resolved to two different namespace I don't see the relevance.
>
> Compare:
>
> b.a += 2
>
> Before the assignment, instance b does not have an attribute "a", so class
> attribute "a" is accessed. You seem to be objecting to this inheritance.

I object to the inheritance in a scope where b.a also refers to the
instance.

If there is no problem that a reference can refer to different objects
in the same scope, then the following should work too.

a = 0
def f():
  a += 2

One can reason just the same that before the assignment f doesn't have
a local variable yet, so the global should be accessed. People who
don't agree don't want functions to have access to outer scope
variables.

> Do you object to import searching multiple directories?
>
> Why do you object to attribute resolution searching multiple namespaces?

I don't.

>> I don't see the relevance of these pieces of code. In none of them is
>> there an occurence of an attribute lookup of the same attribute that
>> resolves to different namespaces.
>
> Look a little more closely. In all three pieces of code, you have a
> conflict between the class attribute 'ls' and an instance attribute 'ls'.

No you look a little more clearly.

> In the first scenario, that conflict is resolved by insisting that
> instances explicitly define an attribute, in other words, by making
> instance attribute ONLY search the instance namespace and not the class
> namespace.

No it isn't. You seem unable to make a difference between a resolution
in general, and a resolution in a scope where an assignment has been
made.

-- 
Antoon Pardon



More information about the Python-list mailing list