Class Variable Access and Assignment
apardon at forel.vub.ac.be
Fri Nov 4 15:58:38 CET 2005
Op 2005-11-04, Christopher Subich schreef <csubich.spam.block at spam.subich.block.com>:
> Antoon Pardon wrote:
>> Op 2005-11-03, Stefan Arentz schreef <stefan.arentz at gmail.com>:
>>>The model makes sense in my opinion. If you don't like it then there are
>>>plenty of other languages to choose from that have decided to implement
>> And again this argument. Like it or leave it, as if one can't in general
>> like the language, without being blind for a number of shortcomings.
>> It is this kind of recations that make me think a number of people is
>> blindly devoted to the language to the point that any criticism of
>> the language becomes intollerable.
> No, it's just that a goodly number of people actually -like- the
> relatively simple conceputal model of Python.
> Why /shouldn't/
> >>>a.x = foo
> correspond exactly to
> >>>setattr(a,'x',foo) #?
> Similarly, why shouldn't
> >>>foo = a.x
> correspond exactly to
> >>>foo = getattr(a,'x') #?
> With that in mind, the logical action for
> >>>a.x = f(a.x)
> >>>setattr(a,'x',f(a,'x')) #,
> and since
> >>>a.x += foo
> is equal to
> >>>a.x = A.__iadd__(a.x,foo) # (at least for new-style classes
> >>> # that have __iadd__ defined. Otherwise, it falls back on
> >>> # __add__(self,other) to return a new object, making this
> >>> # evern more clear),
> why shouldn't this translate into
> >>>setattr(a,'x',A.__iadd__(getattr(a,'x'),foo)) #?
Well maybe because as far as I understand the same kind of logic
can be applied to something like
lst[f()] += foo
In order to decide that this should be equivallent to
lst[f()] = lst[f()] + foo.
But that isn't the case.
So it seems applying augmented operators is not a matter of just
substituting straight translations to get the right result.
> Looking at it this way, it's obvious that the setattr and getattr may do
> different things, if the programmer understands that "instances (can)
> look up object attributes, and (always) set instance attributes." In
> fact, it is always the case (so far as I can quickly check) that += ends
> up setting an instance attribute. Consider this code:
Looking at lists in a similar way, it would be obvious that the
__setitem__ and __getitem__ can do different things and so we
should expect lst[f()] += foo to behave exactly as lst[f()] = lst[f()] +
More information about the Python-list