Label-Value (was: Re: Inheriting the @ sign from Ruby)
Rainer Deyke
root at rainerdeyke.com
Fri Dec 15 11:38:47 EST 2000
"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:91d5el0me7 at news1.newsguy.com...
> "Rainer Deyke" <root at rainerdeyke.com> wrote in message
> news:rWd_5.26820$x6.14119694 at news2.rdc2.tx.home.com...
> [snip]
> > > > class MutableInt(UserInt.UserInt):
> > > > def set(self, value):
> > > > self.data = value
> > >
> > > UserInt is not in the Python 2 docs, though it's nice to hear it
> > exists:-).
> >
> > Actually it doesn't, but it's trivial (if tedious) to implement.
>
> But, if UserInt is not a funky metaclass, how will MutableInt live
> up to its name? Surely it should at least redefine all of the
> in-place methods...:
>
> x = y = MutableInt(23)
> x += 1
Yes, in-place modification should be supported. I had something like this
in mind:
class UserInt:
def __init__(self, value):
self.data = value
def __add__(self, other):
if isinstance(other, UserInt):
return self.__class__(self.data + other.data)
else:
return self.__class__(self.data + other)
__radd__ = __add__
def __iadd__(self, other):
if isinstance(other, UserInt):
self.data += other.data
else:
self.data += other
# Repeat for all other operations
UserInt as written is more properly UserNumber, since it supports longs ints
and floats.
--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the Python-list
mailing list