python metaclass的理解
来源:互联网 发布:怎么登录商家淘宝店铺 编辑:程序博客网 时间:2024/05/21 07:50
python metaclass的理解
1.type,获得变量类型 type(object) -> the object’s type
>>> a = 2>>> type(a)<type 'int'>>>> type(type(a))<type 'type'>>>>>>> class MM(object):... def __init__(self):... self.a = a... >>> mm = MM()>>> type(mm)<class '__main__.MM'>>>> isinstance(mm,MM)True>>> >>>> type(MM)<type 'type'>>>>
- type 生成类 type(name, bases, dict) -> a new type
>>> def func(self):... print "self.a is ",self.a... >>> MyClass = type('MyClass',(object,),{'func':func,'a':100})>>> myclass = MyClass()>>> myclass.func()self.a is 100>>>
- 元类,任何一个类建立的时候都需要元类来创建,元类可以用来改变类
比如下面这个元类就是将类中的变量名改成小写
class UpdateMetaClass(type): def __new__(cls,name,bases,attrslist): attrs = ((name,value) for name,value in attrslist.items() if not name.startswith("__")) lower_attrs = dict((name.lower(),value) for name,value in attrs) #return type.__new__(cls,name,bases,lower_attrs) return super(UpdateMetaClass,cls).__new__(cls,name,bases,lower_attrs)class MyClassTest(object): __metaclass__ = UpdateMetaClass AA = 1000#AA has been changed to be aaprint MyClassTest.aa
0 0
- python metaclass的理解
- python学习笔记-Metaclass理解
- 浅析 Python 的 metaclass
- 浅析python的metaclass
- 浅析python的metaclass
- python的metaclass分析
- python里的metaclass
- 浅析python的metaclass
- 浅析python的metaclass
- 浅析python的metaclass
- metaClass和class的理解
- 深刻理解Python中的元类(metaclass)
- 深刻理解Python中的元类(metaclass)
- 深刻理解Python中的元类(metaclass)
- 深刻理解Python中的元类(metaclass)
- 深刻理解Python中的元类(metaclass)
- 深刻理解Python中的元类(metaclass)
- 深刻理解Python中的元类(metaclass)
- hdoj1095
- 封装viewPager滑动
- ubuntu中配置tftp
- Java的文件读写操作
- SQL Server中,timestamp类型使用注意事项
- python metaclass的理解
- Android快速实现毛玻璃效果
- 整合Struts2 Spring Hibernate,搭建SSH框架
- hdoj1096
- 在通知栏上玩游戏,Steve iOS 游戏实现思路
- 软件设计中为什么会有编码和名称?
- 对javascript封装,继承的理解
- __init
- 行测-言语错题--中心理解