Usefulness of subclassing builtin number types

Lulu of the Lotus-Eaters mertz at gnosis.cx
Sun Dec 15 19:15:16 EST 2002


|* Skip Montanaro <skip at pobox.com> [2002-12-15 17:01 -0600]:
|>     class MyInt(int):
|>         def __imul__(self, other):
|>             return MyInt(self*other)

Gerhard =?iso-8859-1?Q?H=E4ring?= <gerhard.haering at gmx.de> wrote previously:
|I want the objects of my class to keep being objects of my class for
|all artithmetic operations.  At least as long as both operands are
|MyInts.  My complaint was that I have to do this for almost all methods
|of 'int'.  There's little gain here compared to just implementing the
|number protocol.

It would be nice if someone, say Gerhard :), were to create a UserInt
module that defined all those methods.  Much as with UserList, the next
person customizing an int would not need to remember all the methods.
Instead, we could just inherit from UserInt.UserInt, then customize only
the few methods we were interested in.

But this effort is a one-time thing.  The pattern is straightforward,
and it can easily put into a module.

Yours, Lulu...

--
    _/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: Postmodern Enterprises _/_/_/
   _/_/    ~~~~~~~~~~~~~~~~~~~~[mertz at gnosis.cx]~~~~~~~~~~~~~~~~~~~~~  _/_/
  _/_/  The opinions expressed here must be those of my employer...   _/_/
 _/_/_/_/_/_/_/_/_/_/ Surely you don't think that *I* believe them!  _/_/





More information about the Python-list mailing list