Python - metaclass元类
参考
- https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319106919344c4ef8b1e04c48778bb45796e0335839000
- https://zhuanlan.zhihu.com/p/28333506
1. 概念
1.1 MetaClass作用:用来指定当前类由谁来创建(默认type创建)
1.2 cls(metaclass=type)和cls(type)的区别
1.2.1
# 类由type来创建
class Foo(metaclass=type)
# 继承type
class Foo(type)
1.2.2
class Foo(object):
pass
obj = Foo()
# 对象是由类创建
# 一切皆对象,类由type创建
class Foo(object):
pass
# 等价于上面
Foo = type('Foo',(object,),{})
# 一切皆对象,类由MyType创建
# MyType继承了type,里面什么都没写
class MyType(type):
pass
# 相当于type创建这个Foo类
Foo = MyType('Foo',(object,),{})
# 这Foo类是默认由type创建的,如果想要用MyType创建,就要写上metaclass=MyType,如下
class Foo(object):
pass
class Foo(object,metaclass=MyType):
pass
1.2.3
# 一切皆对象,类由MyType创建
class MyType(type):
def __init__(self, *args, **kwargs):
super(MyType, self).__init__(*args, **kwargs)
def __call__(cls, *args, **kwargs):
print('xxx')
return super(MyType, cls).__call__(*args, **kwargs)
# MyType('Foo',(object,),{})等价于class Foo(object,metaclass=MyType)等价于class Foo(metaclass=MyType)
class Foo(object,metaclass=MyType):
pass
# Foo是类,也是对象,Foo()调用创建它的类的__call__ --> Foo = MyType('Foo',(object,),{})
Foo() #输出xxx
2. 如何用metaclass?
第一种为Python3, 第二种为Python2/3
class Foo(metaclass=type):
pass
class Foo(object):
__metaclass__ = type
3. 例子
3.1 MyType('Base', (object,), {}) 是由MyType创建; metaclass=MyType
3.2 type可以创建类时,metaclass=type;MyType创建类时,metaclass=MyType
3.3 Base = MyType('Base', (object,), {}) 等价于 Base(metaclass=MyType)
# 自定义元类
class MyType(type):
def __init__(self, *args, **kwargs):
super(MyType, self).__init__(*args, **kwargs)
def __call__(cls, *args, **kwargs):
print('xxxx')
return super(MyType, cls).__call__(*args, **kwargs)
# 用元类创建Base类,调用了元类的__call__
Base = MyType('Base', (object,), {})
Base()
print(Base) #<class '__main__.Base'>
print(type(Base)) #<class '__main__.MyType'>
# Foo继承Base类,也是指向同一个元类
class Base(metaclass=MyType):
pass
class Foo(Base):
pass
obj = Foo()
结果
xxxx
<class '__main__.Base'>
<class '__main__.MyType'>
xxxx
Python - metaclass元类的更多相关文章
- Python - metaclass元类(图)
个人总结
- python——type()、metaclass元类和精简ORM框架
1.type()函数 if __name__ == '__main__': h = hello() h.hello() print(type(hello)) print(type(h)) Hello, ...
- python中元类(metaclass)的理解
原文地址:http://www.cnblogs.com/tkqasn/p/6524879.html 一:类也是对象 类就是一组用来描述如何生成一个对象的代码. 类也是一个对象,只要你使用关键字clas ...
- python 通过元类控制类的创建
一.python中如何创建类? 1. 直接定义类 class A: a = 'a' 2. 通过type对象创建 在python中一切都是对象 在上面这张图中,A是我们平常在python中写的类,它可以 ...
- Python之元类
类型对象负责创建对象实例,控制对象行为.那么类型对象又由谁来创建呢? 元类(metaclass)——类型的类型 New-Style Class的默认类型是type >>> class ...
- Python 的元类设计起源自哪里?
一个元老级的 Python 核心开发者曾建议我们( 点击阅读),应该广泛学习其它编程语言的优秀特性,从而提升 Python 在相关领域的能力.在关于元编程方面,他的建议是学习 Hy 和 Ruby.但是 ...
- python基础----元类metaclass
1 引子 class Foo: pass f1=Foo() #f1是通过Foo类实例化的对象 python中一切皆是对象,类本身也是一个对象,当使用关键字class的时候,python解释器在加载cl ...
- 谈谈Python中元类Metaclass(一):什么是元类
简单的讲,元类创建了Python中所有的对象. 我们说Python是一种动态语言,而动态语言和静态语言最大的不同,就是函数和类不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个HelloW ...
- 谈谈Python中元类Metaclass(二):ORM实践
什么是ORM? ORM的英文全称是“Object Relational Mapping”,即对象-关系映射,从字面上直接理解,就是把“关系”给“对象”化. 对应到数据库,我们知道关系数据库(例如Mys ...
随机推荐
- layer.open中父页面向子页面传值
1.咱先看图说话 父list.jsp 子operate.jsp实现的代码1 在父页面上完成对子页面的数据渲染 function setData(data) { var lay=layer.open({ ...
- 【C语言】输入5个整数并按输入顺序逆序输出
#include <stdio.h> int main() { ],i; printf("请输入5个整数:\n"); ;i<;i++) scanf("% ...
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- docker安装-单机/多机安装
操作系统ubuntu14.04 16.04 v单机安装步骤: #安装httpsca证书 apt-get install apt-transport-https ca-certificates #添 ...
- 画风清奇!看看大佬怎么玩Python
一提到Python,不少人脑海里都会浮现出几个关键词"数据分析""网络爬虫""人工智能"等,但Python的用法,远不止这些.让我们看看国内 ...
- 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...
- Nexus-FEX基础配置
1.FEX基本配置feature fex fex 100 pinning max-links 1 >>>>该值默认就是1 description "FEX100 ...
- Educational Codeforces Round 82 A. Erasing Zeroes
You are given a string ss. Each character is either 0 or 1. You want all 1's in the string to form a ...
- 【PAT甲级】1055 The World's Richest (25 分)
题意: 输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富.K次询问,每次输入三个正整数M,L,R(M<=100,L,R<= ...
- pip-9.0.1更新到pip-10.0.1遇到的问题
使用 pip 安装第三方库时,报错: You are using pip version 9.0.3, however version 10.0.1 is available. You should ...