设计模式 - Python元类中的__call__和__new__的区别?
大家讲道理
大家讲道理 2017-04-18 10:04:49
0
2
454

参考文档

creating-a-singleton-in-python和what-is-a-metaclass-in-python

问题描述

下面这两段代码的执行结果反映了一个问题:很明显元类的存在会影响__call__和__new__的优先级,请问大神能否分析一下两者执行结果不同的原因?

实例代码

1.不含元类的单例模式

class Singleton(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
        print('__new__')
        return cls._instance

    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
        print('__call__')
        return cls._instance

class Foo(Singleton):
    pass

print('1')
foo1 = Foo()
print('2')
foo2 = Foo()

print(foo1 is foo2)  # True

上面这段代码的执行结果

$ python non_metaclass.py
1
__new__
2
__new__
True

2.含有元类的单例模式

class Singleton(type):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
        print('__new__')
        return cls._instance

    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
        print('__call__')
        return cls._instance


class Foo(metaclass=Singleton):
    # 不兼容Python2
    pass

print('1')
foo1 = Foo()
print('2')
foo2 = Foo()

print (foo1 is foo2)  # True

上面这段代码的执行结果

$ python metaclass.py
__new__
1
__call__
2
__call__
True

如果描述不够详细,请在评论区留一下言,我再改进。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全員に返信(2)
黄舟

我在这里再修改仔细说明下吧

元类是定义类结构属性的, 而类里的 "__new__", "__init__" 方法, 是处理类实例用的

我们所定义的每一个类, 都可以理解为是 type 的一个实例

class Foo(object):
    version = "1.0.1"
    pass

print(Foo.version) # 1.0.1
print(Foo.__name__) # Foo

# ------------
# 使用type创建类是一样的
Foo = type("Foo", (object,), dict(version="1.0.1"))
print(Foo.version)      # 1.0.1
print(Foo.__name__)     # Foo

好, 说回到 "__new__" 和 "__call__"

元类中, "__new__" 会在你定义类的时候执行, 只执行一次, 如果有两个类使用了这个元类, OK, 会执行两次

class FooMeta(type):
    def __new__(meta, name, bases, dct):
        print("metaclass __new__")
        return type(name, bases, dct)

class Foo():
    __metaclass__ = FooMeta
    
# 没有任何代码了, 你会看到 print了metaclass __new__
# 如果你细心, 并联系上面说的, 理解为
# Foo = type("Foo", tuple(), dict())
# 就明白 FooMeta.__new__原来就是 type.__new__
# Foo 是type的一个实例
# 这是为什么定义元类都要base type的原因: class FooMeta(type)
# 如果你定义 __metaclass__ = type 并没什么错, 因为本来默认就是这样

而__call__会在你每次实例化的时候调用, 其实和Foo.__new__是一样的, 意思是, 如果你的Foo定义了__new__, 元类中的__call__便不会执行

class FooMeta(type):
    def __new__(meta, name, bases, dct):
        print("metaclass __new__")
        return type(name, bases, dct)

    def __call__(self, *args, **kwargs):
        print("meta __call__")
        return "hello"


class Foo():
    __metaclass__ = FooMeta

    def __new__(cls, *args, **kwargs):
        print("Foo __new__")
        return "hello"

f = Foo()
print(f)
# 输出结果
# metaclass __new__
# Foo __new__
# hello

元类的 "__new__" 可以变更你所定义的类型, 我在这里定义Foo成了一个list

# -*- coding: utf-8 -*-
class FooMeta(type):
    def __new__(cls, name, bases, dct):
        return [1, 2, 3, 4]

# 这里相当于执行 Foo = [1, 2, 3, 4]
class Foo():
    __metaclass__ = FooMeta

print(Foo)          # [1, 2, 3, 4]
print(Foo[0:2])     # [1, 2]
print(Foo.pop())    # 4

写这么多很辛苦的, 能正解理解metaclass的人非常的少, 不知题主要怎么感谢我

いいねを押す +0
刘奇

这不是元类影响new和call优先级的问题,而是元类在创建类的时候__new__只会被调用一次。而这个__new__就是用来创建出我们的类,如你问题中的。什么时候被创建呢?当解释器解释到Foo类的时候,会在类的定义中寻找__metaclass__属性,如果找到了就用它来创建类。如果没有找到,就会用内建的type来创建这个类。而后为什么不再调用呢,因为元类已经把我们的类创建好了,总不能每次Foo()的时候重新去创建一次类吧。从你的输出中也可以看出foo2 = Foo()并没有输出__new__。因为这个时候类已经被元类即Singleton创建好了,直接开始用就行了。和我们平时用的类没什么区别了,所以每一次创建的都是一个新的对象。而为什么要用__call__呢
因为Foo是元类Singleton创建出来的类,可以认为Foo是Singleton的实例对象。所以每次Foo()的时候都会去调用__call__。而在__call__中我们拿到已经创建好的实例对象。不就是单例吗。。

再通过代码详细说明一下

class Singleton(type):
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            print(cls)
            cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
            print(cls._instance)
        print('__call__')
        return cls._instance


class Foo():
    __metaclass__ = Singleton
    pass

print('1')
foo1 = Foo()
print('2')
foo2 = Foo()

print (foo1 is foo2)  # True

运行之后你会发现,__call__函数中cls是元类第一次也是唯一一次调用__new__创建出来的类即Foo, 而cls._instance则是我们的Foo的实例对象。每次Foo()的时候都是取的同一实例对象。 元类毕竟是创建类的类。一旦创建好了,类就和平时定义的类没有什么两样。

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!