Label-Value (was: Re: Inheriting the @ sign from Ruby)

Alex Martelli aleaxit at yahoo.com
Sun Dec 17 04:43:54 EST 2000


"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:JnW_5.37708$x6.18119061 at news2.rdc2.tx.home.com...
    [snip]
> > >class UserInt:
    [snip]
> > >  def __iadd__(self, other):
    [snip]
> > Just as a matter of style, would it be superior to add a __coerce__
method
> > that knows how to coerce ints, longs, and floats into UserInts, or make
an
    [snip]
> __coerce__ would probably have been superior, since it eliminates
duplicate
> code and more closely resembles the way real integers work in Python.

__coerce__ is what I had used in my generic Mutable class, previously
posted, together with a __getattr__ and a few others, to try and mimic
a variety of objects it could be holding by delegating stuff to whatever
it's holding in a way reasonably economical of code.

But it doesn't really work for __iadd__ and friends, does it?  Unless I'm
missing something, those have to be implemented one by one.  Maybe
__getattr__ tricks could help -- if name.startswith('__i'), one could use
name.replace('__i','__',1) to look a function up in library module
'operator', etc.  But that doesn't exactly feel very 'clean' to me.  (The
performance issues could be ameliorated by having Mutable instances
cache a directory of such methods once looked-up and built, but that
doesn't make the overall architecture any cleaner, it seems to me).


Alex






More information about the Python-list mailing list