Returning other instance from __init__ - I need help

Paulo da Silva psdasilvaX at esotericaX.ptX
Sat Mar 17 02:05:05 CET 2007


Paulo da Silva escreveu:
> Paulo da Silva escreveu:
>> Alex Martelli escreveu:
>>> Paulo da Silva <psdasilvaX at esotericaX.ptX> wrote:
>> ....
>>
>>> E.g.:
>>>
>>> class C1(object):
>>>     def __new__(cls, xxx):
>>>         if xxx: return type.__new__(cls, xxx)
>>>         else: return C1.load(xxx)
>>>     @staticmethod
>>>      def load(xxx): return ...whatever...
>>>      def __init__(self, xxx):
>>>          if hasattr(self, 'foo'): return
>>>          self.foo = 'foo'
>>>          self.bar = 'bar'
>>>
>>
>> Just for a better understanding ...
>> Can I do this?
>>
>> class C1(object):
>>     def __new__(cls, xxx):
>>         if xxx:
>> 		cls.foo='foo'
>> 		cls.bar='bar'
>> 		return type.__new__(cls, xxx)
> This does not work(!) at least for python 2.4.3.
> 
>>         else:
>> 		return C1.load(xxx)
>>     @staticmethod
>>     def load(xxx): return ...whatever...
>>     # OMMIT THE __init__
>>     # or
>>     def __init__(self, xxx):
>>         pass
> 
> I needed return cls and put the __init__ stuff here.
> Is this the best practice?
> 

When debugging, I found this is wrong!!!
Would someone please clarify what do I have to return from
__new__?

Thank you very much.





More information about the Python-list mailing list