return an object of a different class
jeanmichel at sequans.com
Thu Feb 17 06:02:28 EST 2011
>> 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
>> cls = BigNumbers
>> return object.__new__(cls, n)
> Very beautiful code great alternative to factory method!
> To memorize this pythonic way.
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)
More information about the Python-list