def new_wrapped_instance(cls, *args, **kwargs): """ obtain a better wrapped instance, experimental, doesnt work well on py2 """ func = args and args[0] or None i = object.__new__( type(cls.__name__ + '_' + func.__name__, (cls, ), {'__doc__': func.__doc__})) cls.__init__(i, *args, **kwargs) return i
def __new__(cls, *p, **k): if cls != type(cls.__instance): __instance = object.__new__(cls) #srubio: added init_single check to prevent redundant __init__ calls if hasattr(cls,'__init__') and cls.__init__ != cls.__dumb_init: setattr(cls,'init_single',cls.__init__) setattr(cls,'__init__',cls.__dumb_init) #Needed to avoid parent __init__ methods to be called if hasattr(cls,'init_single'): cls.init_single(__instance,*p,**k) #If no __init__ or init_single has been defined it may trigger an object.__init__ warning! cls.__instance = __instance #Done at the end to prevent failed __init__ to create singletons return cls.__instance
def __new__(cls, *p, **k): key = cls.parse_instance_key(*p,**k) if cls != type(cls.__instances.get(key)): __instance = object.__new__(cls) __instance.__instance_key = key #srubio: added init_single check to prevent redundant __init__ calls if hasattr(cls,'__init__') and cls.__init__ != cls.__dumb_init: setattr(cls,'init_single',cls.__init__) setattr(cls,'__init__',cls.__dumb_init) #Needed to avoid parent __init__ methods to be called if hasattr(cls,'init_single'): cls.init_single(__instance,*p,**k) #If no __init__ or init_single has been defined it may trigger an object.__init__ warning! cls.__instances[key] = __instance #print '#'*80+'\n'+'%s.__instances[%s] = %s'%(str(cls),key,str(__instance)) return cls.__instances[key]
def __new__(cls, value): print(cls, value) __instance = object.__new__(cls, value) cls.__init__(__instance, value) return __instance.value