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

Quinn Dunkan quinn at
Sun Dec 17 03:06:55 CET 2000

On Fri, 15 Dec 2000 16:38:47 GMT, Rainer Deyke <root at> wrote:
>Yes, in-place modification should be supported.  I had something like this
>in mind:
>class UserInt:
>  def __init__(self, value):
> = value
>  def __add__(self, other):
>    if isinstance(other, UserInt):
>      return self.__class__( +
>    else:
>      return self.__class__( + other)
>  __radd__ = __add__
>  def __iadd__(self, other):
>    if isinstance(other, UserInt):
> +=
>    else:
> += other
>  # Repeat for all other operations
>UserInt as written is more properly UserNumber, since it supports longs ints
>and floats.

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
explicit check on each method?  __coerce__ saves a lot of typing, but I'm
generally wary of implicit things like coercion.  When I implemented a class
like this (a vector, actually), I used __coerce__, but after trying to wrap my
brain around the control flow, I was thinking maybe I wouldn't next time, even
though it seems to work fine.

Is this fear unfounded?  Is there a better way?

More information about the Python-list mailing list