[Python-Dev] Re: Int FutureWarnings and other 2.4 TODOs

Guido van Rossum guido at python.org
Thu Dec 4 00:05:18 EST 2003

> If one accepts that int and long have the same operations but offer
> different implementations, then the obvious solution is: an abstract
> base class (Int) and two sub-classes (Short and Long?).

Right, this seems to be one of the preferred solutions.

> Of course, this obvious solution has a problem: a programmer can't
> easily sub-class Int.  They must choose one a sub-class from Short
> or Long (unless they want to provide a full implementation
> themselves) and that's probably not a decision they want to make.

(It has been pointed out that subclassing int isn't very useful, so
maybe this is a moot point.  Does anybody have a real use case?)

> Perhaps, because of this, the GOF "Bridge Pattern" might be suitable
> here.  (This pattern can be framed as: "adapt multiple
> implementations to an interface using delegation" -- which, after
> all, is pretty much what the vtable in previous solution gives you.)
> If the existence of Short and Long is an implementation detail best
> hidden from the python programmer, then a Bridge Pattern
> implementation has a lot going for it.

Hmm...  I'm not very familiar with the Bridge pattern (and my GoF book
is on one of 65 boxes still in the garage waiting until we move into a
larger house :-).  Can you give a little more detail about how this
would be done?  Is it something that a user wishing to subclass Int
should do, or something that the Int class (or its subclasses) should

> Use of the Bridge pattern might even allow for three different
> implementations: PreDefined, Short, Long ... where PreDefined is one
> of the numbers between 0 and 256 which I'm given to understand are
> pre-allocated by the runtime environment.

I don't think so -- the beauty is that (most of) the implementation
doesn't know that certain int values have eternal life; their
representation is exactly the same as that of regular ints.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list