Returning other instance from __init__

Paulo da Silva psdasilvaX at esotericaX.ptX
Thu Mar 15 10:54:28 EDT 2007


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)
        else:
		return C1.load(xxx)
    @staticmethod
    def load(xxx): return ...whatever...
    # OMMIT THE __init__
    # or
    def __init__(self, xxx):
        pass



More information about the Python-list mailing list