piaochen
V2EX  ›  Python

Python2.7 和 3.6, metaclass 机制有何不同?

By piaochen at 2017 年 8 月 7 日 · 4331 次点击
这两天对 metaclass 查了一些材料,其中有一篇关于使用 metaclass 的方式,实现单利,代码如下:


# -*- coding:utf-8 -*-
class Singleton(type):
def __init__(self, *args, **kwargs):
print("__init__")
self.__instance = None
super(Singleton,self).__init__(*args, **kwargs)

def __call__(self, *args, **kwargs):
print("__call__")
if self.__instance is None:
self.__instance = super(Singleton,self).__call__(*args, **kwargs)
return self.__instance

class Foo(object):
__metaclass__ = Singleton

foo1 = Foo()
foo2 = Foo()
print(Foo.__dict__)
print(id(foo1))
print(id(foo2))
print(foo1.__class__)
print(foo1.__class__.__class__)


我发现这段代码,在 py3.6 下,foo1 和 foo2 的 id 不一样,Singleton 的 init 和 call 方法也没调用。foo1.__class__.__class__是 type

我切换到 py2.7 下
这段代码运行正常,单例也构建成功。id(foo1)==id(foo2),init 和 call 方法也都调用了。
关键是 foo1.__class__.__class__为 Singleton.

根据我的理解,我为 Foo 指定了 metaclass 为 Singleton,那么 Foo.__class__就应该是 Singleton,也就是 py2.7 的逻辑。但是 py3.6 中表现出来的,就不是这样了,指定了 metaclass,Foo.__metaclass__还是 type,metaclass 只不过相重写了 Foo 一些方法而已。不知道我的理解对不对。

py3 以后对 metaclass 的机制到底是怎么样的?
3 条回复  •  2017-08-07 21:21:29 +08:00
kunimi
   1
kunimi  
   2017 年 8 月 7 日
这排版。。。
在 python 3 中,定义 metaclass 的语法变了,改成下面的在 python 3 中运行就没问题了
class Foo(metaclass=Singleton ): pass
piaochen
   2
piaochen  
OP
   2017 年 8 月 7 日
@kunimi 试了下,果然如此,非常感谢。
wentian
   3
wentian  
   2017 年 8 月 7 日
变化很多, Python 3 系列更加直观
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
© 2026 V2EX · 35ms · 3.9.8.5