return an object of a different class

Jean-Michel Pichavant jeanmichel at sequans.com
Thu Feb 17 12:02:28 CET 2011


Karim wrote:
> [snip]
>> 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)
>>      ...
>>
>> Chard.
>
> Very beautiful code great alternative to factory method!
> To memorize this pythonic way.
>
> Regards
> Karim
Do you think  that the MyNumber constructor returning something else 
than a MyNumber instance is the pythonic way ? It would rather be the 
cryptonic way ! (haha)

JM



More information about the Python-list mailing list