return an object of a different class

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


Richard Thomas <chardster at gmail.com> 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 vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
Nu Fair Trade woonartikelen op http://www.zylja.com



More information about the Python-list mailing list