Class Variable Access and Assignment
apardon at forel.vub.ac.be
Fri Nov 4 09:08:42 CET 2005
Op 2005-11-03, Steven D'Aprano schreef <steve at REMOVETHIScyber.com.au>:
> On Thu, 03 Nov 2005 04:30:09 -0800, Paul Rubin wrote:
>> Steve Holden <steve at holdenweb.com> writes:
>>> > class A:
>>> > a = 1
>>> > b = A()
>>> > b.a += 2
>>> > print b.a
>>> > print A.a
>>> > Which results in
>>> > 3
>>> > 1
>>> I don't suppose you'd care to enlighten us on what you'd regard as the
>>> superior outcome?
>> class A:
>> a = 
>> b = A()
>> print b.a
>> print a.a
>> Compare and contrast.
> I take it then that you believe that ints like 1 should be mutable like
> lists? Because that is what the suggested behaviour implies.
No it isn't.
One other way, to implement the += and likewise operators would be
something like the following.
Assume a getnsattr, which would work like getattr, but would also
return the namespace where the name was found. The implementation
of b.a += 2 could then be something like:
ns, t = getnsattr(b, 'a')
t = t + 2
I'm not arguing that this is how it should be implemented. Just
showing the implication doesn't follow.
More information about the Python-list