我只是试图简化我的一个类,并引入了一些与flyweight设计模式相同风格的功能。

然而,我有点困惑,为什么__init__总是在__new__之后被调用。我没想到会这样。有人能告诉我为什么会发生这种情况,我如何才能实现这个功能吗?(除了将实现放在__new__中,这感觉相当粗糙。)

这里有一个例子:

class A(object):
    _dict = dict()

    def __new__(cls):
        if 'key' in A._dict:
            print "EXISTS"
            return A._dict['key']
        else:
            print "NEW"
            return super(A, cls).__new__(cls)

    def __init__(self):
        print "INIT"
        A._dict['key'] = self
        print ""

a1 = A()
a2 = A()
a3 = A()

输出:

NEW
INIT

EXISTS
INIT

EXISTS
INIT

Why?


当前回答

我认为这个问题的简单答案是,如果__new__返回一个与类类型相同的值,__init__函数将执行,否则它不会执行。在这种情况下,您的代码返回A._dict('key'),它与cls是同一个类,因此将执行__init__。

其他回答

再深入一点!

CPython中泛型类的类型是type,它的基类是Object(除非你显式地定义了另一个基类,比如元类)。低级调用的序列可以在这里找到。第一个调用的方法是type_call,然后调用tp_new和tp_init。

这里有趣的部分是tp_new将调用对象的(基类)new方法object_new,该方法执行tp_alloc (PyType_GenericAlloc),为对象分配内存:)

此时在内存中创建对象,然后调用__init__方法。如果__init__没有在你的类中实现,那么object_init会被调用,它什么都不做:)

然后type_call只返回绑定到变量的对象。

参考这个文档:

在继承不可变内置类型(如数字和字符串)时, 偶尔在其他情况下,会出现静态方法__new__ 派上了用场。__new__是实例构造的第一步,被调用 __init__之前。

__new__方法是用类来调用的 第一个参数;它的职责是返回该函数的一个新实例 类。

将其与__init__进行比较:__init__是通过实例调用的 作为它的第一个参数,它不返回任何东西;它的 职责是初始化实例。

有一些情况 在没有调用__init__(例如 当实例从pickle加载时)。没有办法去创造 一个不调用__new__的新实例(尽管在某些情况下可以 不需要调用基类的__new__)。

关于你想要达到的目标,还有关于单例模式的相同文档信息

class Singleton(object):
        def __new__(cls, *args, **kwds):
            it = cls.__dict__.get("__it__")
            if it is not None:
                return it
            cls.__it__ = it = object.__new__(cls)
            it.init(*args, **kwds)
            return it
        def init(self, *args, **kwds):
            pass

您也可以使用PEP 318中的这个实现,使用装饰器

def singleton(cls):
    instances = {}
    def getinstance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return getinstance

@singleton
class MyClass:
...

引用文件:

Typical implementations create a new instance of the class by invoking the superclass's __new__() method using "super(currentclass, cls).__new__(cls[, ...])"with appropriate arguments and then modifying the newly-created instance as necessary before returning it. ... If __new__() does not return an instance of cls, then the new instance's __init__() method will not be invoked. __new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation.

在大多数知名的OO语言中,像SomeClass(arg1, arg2)这样的表达式将分配一个新实例,初始化实例的属性,然后返回该实例。

在大多数知名的OO语言中,“初始化实例的属性”部分可以通过定义构造函数来为每个类定制,构造函数基本上就是在新实例上操作的代码块(使用提供给构造函数表达式的参数),以设置所需的任何初始条件。在Python中,这对应于类的__init__方法。

Python的__new__就是类似的“分配新实例”部分的每个类自定义。当然,这允许您做一些不寻常的事情,比如返回一个现有的实例,而不是分配一个新实例。所以在Python中,我们不应该认为这部分涉及到分配;我们所需要的只是__new__从某个地方提供一个合适的实例。

但这仍然只是工作的一半,Python系统无法知道有时你想在之后运行工作的另一半(__init__),有时你不想。如果你想要这种行为,你必须明确地说出来。

通常,你可以重构,这样你只需要__new__,或者你不需要__new__,或者让__init__在一个已经初始化的对象上表现不同。但如果你真的想这样做,Python实际上允许你重新定义“作业”,这样SomeClass(arg1, arg2)就不一定会调用__new__后跟__init__。要做到这一点,你需要创建一个元类,并定义它的__call__方法。

A metaclass is just the class of a class. And a class' __call__ method controls what happens when you call instances of the class. So a metaclass' __call__ method controls what happens when you call a class; i.e. it allows you to redefine the instance-creation mechanism from start to finish. This is the level at which you can most elegantly implement a completely non-standard instance creation process such as the singleton pattern. In fact, with less than 10 lines of code you can implement a Singleton metaclass that then doesn't even require you to futz with __new__ at all, and can turn any otherwise-normal class into a singleton by simply adding __metaclass__ = Singleton!

class Singleton(type):
    def __init__(self, *args, **kwargs):
        super(Singleton, self).__init__(*args, **kwargs)
        self.__instance = None
    def __call__(self, *args, **kwargs):
        if self.__instance is None:
            self.__instance = super(Singleton, self).__call__(*args, **kwargs)
        return self.__instance

然而,这可能比在这种情况下真正被保证的更深层的魔法!

原因很简单,new用于创建实例,而init用于初始化实例。在初始化之前,应该先创建实例。这就是为什么new应该在init之前调用。