元类 metaclass
metaclass
- 类由Type创建
- 对象由创建
MetaClass作用
用来指定当前类由谁来创建(默认type创建)。
MetaClass 会被继承,如果父类指定了元类,那么子类也是由这个元类创建
class MyType(type):
def __init__(self, *args, **kwargs):
print('init')
super(MyType, self).__init__(*args, **kwargs) def __call__(self, *args, **kwargs):
print('call本质:调用类的__new__,再调用类的__init__')
return super(MyType, self).__call__(*args, **kwargs) class Foo(metaclass=MyType):
pass class Bar(Foo):
pass obj = Bar() # init
# init
# call本质:调用类的__new__,再调用类的__init__
1. 创建类的两种方法
#class创建 class Foo(object):
pass #type创建 #运用type创建类、添加属性
# type(类名,(继承的类),{属性和方法,用键值对的方式})
Test = type("Test",(),{'age':13,'name':"wangbo"})
test = Test()
print(test.age) #
#利用type添加方法
@classmethod #类方法
def testClass(cls):
print(cls.name) @staticmethod #静态方法
def testStatic():
print("static method.....") def echo_age(self):
print(self.age)
#
Test2 = type("Test2",(Test,),{'echo_age':echo_age,'testStatic':testStatic,'testClass':testClass})
test2 = Test2()
test2.echo_age()
test2.testStatic()
test2.testClass()
2. 调用顺序
class MyType(type):
def __init__(self, *args, **kwargs):
print("元类init")
super(MyType, self).__init__(*args, **kwargs) def __new__(cls, *args, **kwargs):
print("new")
return super().__new__(cls, *args, **kwargs) def __call__(self, *args, **kwargs):
print("call")
obj = self.__new__(self, *args, **kwargs) # object.__init__(....)
obj.__init__(*args, **kwargs)
# self.__init__(obj,*args, **kwargs)
return obj class Foo(object, metaclass=MyType):
def __init__(self):
print('本类init') x = Foo() # new
# 元类init
# call
# 本类init # 调用顺序:创建一个类调时候先调用他元类的__new__,然后元类的__init__,实例化这个类的时候调用元类的__call__,
# 在__call__中调用这个类的__new__然后__init__得到一个对象
# 类是Mytype的对象,实例化时加括号调用__call__
3. 设置元类
py3
class Foo(object,metaclass=MyType):
pass py2
class Foo(object):
__metaclass__ = MyType
元类 metaclass的更多相关文章
- 深刻理解Python中的元类metaclass(转)
本文由 伯乐在线 - bigship 翻译 英文出处:stackoverflow 译文:http://blog.jobbole.com/21351/ 译注:这是一篇在Stack overflow上很热 ...
- 深刻理解Python中的元类(metaclass)
译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...
- Python中的元类(metaclass)
推荐+收藏:深刻理解Python中的元类(metaclass) 做一些笔记学习学习: 在大多数编程语言中,类就是用来描述如何生成一个对象的代码段,在Python中类也是一个对象,这个(类)对象自身拥有 ...
- [转] 深刻理解Python中的元类(metaclass)
非常详细的一篇深入讲解Python中metaclass的文章,感谢伯乐在线-bigship翻译及作者,转载收藏. 本文由 伯乐在线 - bigship 翻译.未经许可,禁止转载!英文出处:stacko ...
- [转]深刻理解Python中的元类(metaclass)以及元类实现单例模式
使用元类 深刻理解Python中的元类(metaclass)以及元类实现单例模式 在看一些框架源代码的过程中碰到很多元类的实例,看起来很吃力很晦涩:在看python cookbook中关于元类创建单例 ...
- 深刻理解Python中的元类(metaclass)【转】
译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...
- 深入理解Python中的元类(metaclass)
原文 译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍 ...
- python——深刻理解Python中的元类(metaclass)
译注:这是一篇在Stack overflow上 很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉 ...
- python中的元类metaclass
本文是一个转载的,因为原文写的太好了,所以直接copy过来吧. 原文请看:http://blog.jobbole.com/21351/ 译注:这是一篇在Stack overflow上 很热的帖子.提问 ...
- 谈谈Python中元类Metaclass(二):ORM实践
什么是ORM? ORM的英文全称是“Object Relational Mapping”,即对象-关系映射,从字面上直接理解,就是把“关系”给“对象”化. 对应到数据库,我们知道关系数据库(例如Mys ...
随机推荐
- 002servlet生命周期以及有关servlet的各种知识
4 Sevlet的生命周期(重点) 有关servlet的类有Servlet,HttpServlet以及GenericServlet. 其实我们要写一个Servlet只要写一个类去实现Servet就可以 ...
- Oracle 10g 数据库手动创建步骤
Oracle 数据库手动创建步骤 编写初始化参数文件 设置操作系统环境变量 创建实例 以管理员身份连接数据库 启动实例 create database 创建数据库 运行数据字典脚本 Oracle的网络 ...
- JSON.parse() 和 JSON.stringify() 的区别
JSON.parse()与JSON.stringify()的区别 JSON.parse()[从一个字符串中解析出json对象] //定义一个字符串 var data='{"name&qu ...
- Dynamics CRM 2015 Update 1 系列(3): API的那些事 - Old APIs VS New APIs
今天我们来看看API的变化.新系统中,去掉了一些经常使用的数据处理API,比如:SetStateRequest, SetBusinessUnitRequest, SetParentBusinessUn ...
- IOS 开发之--获取真机的deviceToeken
获取真机的devicetoken的方法: #pragma mark 注册APNs成功并上报DeviceToken - (void)application:(UIApplication *)applic ...
- SQL 使用序列
SQL 使用序列 序列是根据需要产生的一组有序整数:1, 2, 3 ... 序列在数据库中经常用到,因为许多应用要求数据表中的的每一行都有一个唯一的值,序列为此提供了一种简单的方法. 本节阐述在 My ...
- 20分钟成功编写bootstrap响应式页面 就这么简单
最近发现一个叫 Bootstrap 的好东西,Bootstrap 是现在最流行的响应式 CSS 框架,它以移动设备优先,能够快速适应不同设备.使用它编写响应式页面快捷.方便,而且屏蔽了浏览器差异.使用 ...
- 【BZOJ3436】小K的农场 差分约束
[BZOJ3436]小K的农场 Description 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了, ...
- you *might* want to use the less safe log_bin_trust_function_creators variable
报错:you *might* want to use the less safe log_bin_trust_function_creators variable 解决方法如下: 1. mysql&g ...
- LeetCode-Count Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...