Class Variable Access and Assignment
Mike Meyer
mwm at mired.org
Fri Nov 4 18:06:25 EST 2005
Antoon Pardon <apardon at forel.vub.ac.be> writes:
> Op 2005-11-04, Mike Meyer schreef <mwm at mired.org>:
>>
>>> Would it be too much to ask that in a line like.
>>>
>>> x = x + 1.
>>>
>>> both x's would resolve to the same namespace?
>>
>> Yes. That's to much bondage for programmers who've become accustomed
>> to freedom. Explain why this should be illegal:
>>
>>>>> class C:
>> ... def __getattr__(self, name):
>> ... x = 1
>> ... return locals()[name]
>> ... def __setattr__(self, name, value):
>> ... globals()[name] = value
>> ...
>>>>> o = C()
>>>>> o.x = o.x + 1
>>>>> x
>> 2
>
> I'll answer with a contra question.
>
> Please explain why this is illegal.
>
> x = 1
> def f():
> x += 1
>
> f()
It isn't illegal, it just requires a different syntax.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list