return an object of a different class

Piet van Oostrum piet at
Sat Feb 19 15:22:20 EST 2011

Richard Thomas <chardster at> writes:

> If you don't want to use a factory function I believe you can do this:
> class MyNumber(object):
>     def __new__(cls, n):
>         if n <= 100:
>             cls = SmallNumbers
>         else:
>             cls = BigNumbers
>         return object.__new__(cls, n)

But you have to be aware that the initializers are not called in this way.

Piet van Oostrum <piet at>
PGP key: [8DAE142BE17999C4]
Nu Fair Trade woonartikelen op

More information about the Python-list mailing list