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 ...
随机推荐
- 《实战Java高并发程序设计》读书笔记三
第三章 JDK并发包 1.同步控制 重入锁:重入锁使用java.util.concurrent.locks.ReentrantLock类来实现,这种锁可以反复使用所以叫重入锁. 重入锁和synchro ...
- Eclipse安装配置java项目
设置智能提示
- JAVA常量池、栈、堆的比较(转载)
今天在学JAVA的数据存储位置的时候,看到了一篇博文感觉不错,特此转载: http://www.cnblogs.com/Eason-S/p/5658230.html JAVA中,有六个不同的地方可以存 ...
- 【struts 报错】 No action config found for the specified url
1 type Exception report message org.apache.struts.chain.commands.InvalidPathException: No action con ...
- A Simple Problem with Integers(树状数组区间变化和区间求和)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- 【C语言】创建一个函数,将输入的2个数排序
#include <stdio.h> void fun(int *x,int*y) { int t; if(*x>=*y) { t=*x;*x=*y;*y=t; } } main() ...
- mysql 存入数据库 中文乱码
1.要保证数据库.表.字段都是utf-8的数据类型.排序一直即可. 数据库的在数据库属性里面改: 表的在设计表里面改: 字段的也是在设计表里面改: 常用命令: -- 检查字符集类型show varia ...
- 计算机二级-C语言-程序填空题-190117记录-对文件的处理,复制两个文件,往新文件中写入数据。
//给定程序的功能是,调用函数fun将指定源文件中的内容赋值到指定目标文件中,复制成功时函数返回1,失败时返回0,把复制的内容输出到终端屏幕.主函数中源文件名放在变量sfname中,目标文件名放在变量 ...
- 大端(bigend)与小端(littleend)
大端:是指数据的低位保存在内存的高地址中,而数据的高位,保存在内存的低地址中: 小端:是指数据的高位保存在内存的高地址中,而数据的高 ...
- 排序算法之选择排序的python实现
选择排序算法的工作原理如下: 1. 首先在序列中找到最小或最大元素,存放到排序序列的前或后. 2. 然后,再从剩余元素中继续寻找最小或最大元素. 3. 然后放到已排序序列的末尾. 4. 以此类推,直到 ...