- 浏览: 399551 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
the base article is available here: http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python
to understand the metaclass in Python, there is some background that you should be aware in order to grasp the idea of metaclass and its foundation.
Part I:
Classes as object
Given an class declaration:
>>> class ObjectCreator(object): ... pass ... >>> my_object = ObjectCreator() >>> print my_object <__main__.ObjectCreator object at 0x8974f2c>
Classes in Python has more... put it simple. that Classes in Python are objects too.
As soon as keyword class, Python execute it and craete an OBJECT. the instruction
>>> class ObjectCreator(object): ... pass ...
create in memory an object with name ObjectCreator.
what you can do with the Class object.
- you can assign it to a variable
- you can copy it
- you can add attributes to it
- you can pass it as a function parameter
here is an example of how you can do with the class object.
>>> print ObjectCreator # you can print a class because it's an object <class '__main__.ObjectCreator'> >>> def echo(o): ... print o ... >>> echo(ObjectCreator) # you can pass a class as a parameter <class '__main__.ObjectCreator'> >>> print hasattr(ObjectCreator, 'new_attribute') False >>> ObjectCreator.new_attribute = 'foo' # you can add attributes to a class >>> print hasattr(ObjectCreator, 'new_attribute') True >>> print ObjectCreator.new_attribute foo >>> ObjectCreatorMirror = ObjectCreator # you can assign a class to a variable >>> print ObjectCreatorMirror.new_attribute foo >>> print ObjectCreatorMirror() <__main__.ObjectCreator object at 0x8997b4c>
Part II:
Creating Classes Dynamically
In the base article, there is a example demonstrate how to create classes inside a function. But that is not dynamic.
>>> def choose_class(name): ... if name == 'foo': ... class Foo(object): ... pass ... return Foo # return the class, not an instance ... else: ... class Bar(object): ... pass ... return Bar ... >>> MyClass = choose_class('foo') >>> print MyClass # the function returns a class, not an instance <class '__main__.Foo'> >>> print MyClass() # you can create an object from this class <__main__.Foo object at 0x89c6d4c>
Since classes are object, they must be generated by something. the somehting here is the metaclass.
But before we drill down to the Metaclass, let's close exame type keyword .
The following shows what typically you do with type, you get what type an objects is.
>>> print type(1) <type 'int'> >>> print type("1") <type 'str'> >>> print type(ObjectCreator) <type 'type'> >>> print type(ObjectCreator()) <class '__main__.ObjectCreator'>
However, type has more, it has a completely different ability, it can also create classes on the fly...
the function prototyp of type that create types on the fly is as follow.
type(name of the class, tuple of the parent class (for inheritance, can be empty), dictionary containing attributes names and values)
e.g
>>> class MyShinyClass(object): ... pass ... >>> print type(MyShinyClass) <type 'type'> >>> MyShinyClass2 = type('MyShinyClass2', (), {}) >>> print MyShinyClass2 >>> # and then you can create the instance of the dynamically created class ... >>> print MyShinyClass2() <__main__.MyShinyClass2 object at 0x0110CCB0># and then you can cre
So, the following are equivalent.
>>> class Foo(object): ... bar = True
and
>>> Foo = type('Foo', (), {'bar':True})
and after you created Foo (with the type keyword), you uses it as if you have decalred it.
>>> print Foo <class '__main__.Foo'> >>> print Foo.bar True >>> f = Foo() >>> print f <__main__.Foo object at 0x8a9b84c> >>> print f.bar True
Part III:
What are metaclass (finally)
The metaclass, boils down to the following definition:
Metaclasses are the 'stuff' that creates classes, since classes create objects (as well as being objects), so you take that meta classes as class's class
MyClass = MetaClass() MyObject = MyClass()
the following shows step by step by examing the internals of the type system in Python.
A speical note on type method itself.
Checking the __class__ attribute.
>>> age = 35 >>> age.__class__ <type 'int'> >>> name = 'bob' >>> name.__class__ <type 'str'> >>> def foo(): pass >>> foo.__class__ <type 'function'> >>> class Bar(object): pass >>> b = Bar() >>> b.__class__ <class '__main__.Bar'>
But what is the __class__ of the __class__, see following..
>>> a.__class__.__class__ <type 'type'> >>> age.__class__.__class__ <type 'type'> >>> foo.__class__.__class__ <type 'type'> >>> b.__class__.__class__ <type 'type'>
So, they are 'type'.
From the examles above. we can see that a meta class is a "class factory", "type" is the built-in metaclass pythons ues, but of course, you can create your own metaclasses.
Part IV:
the __metaclass__ attribute
the form
class Foo(object): __metaclass__ = something... [...]
remember we said before that Pyton will translate the class declaration to an object, but wait, when there is a __metaclass__ attribute.
Python willl look for __metaclass__ in the class definition. If it finds it, if will use it to create the object classFoo
. If it doesn't, it will use type
to create the class.
The order of searching the __metaclass__ is as follow.
- Is there a __metaclass__ attribute in Foo?
- If yes, create in memory a class object (I said a class object, stay with me here), with the nameFoo by using what is in __metaclass__.
- If Python can't find __metaclass__, it will look for a __metaclass__ in Bar (the parent class), and try to do the same.
- If Python can't find __metaclass__ in any parent, it will look for a __metaclass__ at the MODULE level, and try to do the same.
- Then if it can't find any __metaclass__ at all, it will use type to create the class object.
Part V:
What to put in a custom metaclass
Let's see a stupid example.
Suppose we are creating a metaclasses which will alternate all the attributes of classes that it create to have attributes of the classes to be upper cases.
The following shows how to do that via creating a function as the __metaclass__ (__metaclass__ does not necessary mean it has to be a class) at the module level,
# the metaclass will automatically get passed the same argument # that you usually pass to `type` def upper_attr(future_class_name, future_class_parents, future_class_attr): """ Return a class object, with the list of its attribute turned into uppercase. """ # pick up any attribute that doesn't start with '__' attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__')) # turn them into uppercase uppercase_attr = dict((name.upper(), value) for name, value in attrs) # let `type` do the class creation return type(future_class_name, future_class_parents, uppercase_attr) __metaclass__ = upper_attr # this will affect all classes in the module class Foo(object): # we can define __metaclass__ here instead to affect only this class bar = 'bip' print hasattr(Foo, 'bar') # Out: False print hasattr(Foo, 'BAR') # Out: True f = Foo() print f.BAR # Out: 'bip'
Later, let's do that via the class defintinition
# remember that `type` is actually a class like `str` and `int` # so you can inherit from it class UpperAttrMetaclass(type): # __new__ is the method called before __init__ # it's the method that creates the object and returns it # while __init__ just initializes the object passed as parameter # you rarely use __new__, except when you want to control how the object # is created. # here the created object is the class, and we want to customize it # so we override __new__ # you can do some stuff in __init__ too if you wish # some advanced use involves overriding __call__ as well, but we won't # see this def __new__(upperattr_metaclass, future_class_name, future_class_parents, future_class_attr): attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__')) uppercase_attr = dict((name.upper(), value) for name, value in attrs) return type(future_class_name, future_class_parents, uppercase_attr)
If we apply all the OOP technique (like the super keyword, calling to base __new__ and applies the convention names such as "cls", "base" and etcc..), then we can get the following code .
class UpperAttrMetaclass(type): def __new__(cls, name, bases, dct): attrs = ((name, value) for name, value in dct.items() if not name.startswith('__')) uppercase_attr = dict((name.upper(), value) for name, value in attrs) return super(UpperAttrMetaclass, cls).__new__(cls, name, bases, uppercase_attr)
So in a conclusion, about __metaclass__
indeed, metaclasses are especially useful to do black magic, and therefore complicated stuff. But by themselves, they are simple:
- intercept a class creation
- modify the class
- return the modified class
why the hell would you use metaclass?
class Person(models.Model): name = models.CharField(max_length=30) age = models.IntegerField()
guy = Person(name='bob', age='35') print guy.age
IntegerField
object. It will return an int
, and can even take it directly from the database.
models.Model
defines __metaclass__
and it uses some magic that will turn the Person
you just defined with simple statements into a complex hook to a database field.
the last word:
Everything is an object in Python, and they are all either instances of classes or instances of metaclasses.
- monkey patching
- class decorators
发表评论
-
python defining functions and calling functions
2013-04-02 16:19 693Python actually allow very fl ... -
python - trick to load kerberos urllib2
2012-10-17 11:49 0Currently there is a bug that k ... -
python - get the modification time and creation time of a file
2012-09-12 12:12 873as in Unix, you know that the c ... -
Use of Python metaclass II - Python Enumeration
2012-07-02 15:00 978In my previous post, I disusses ... -
Use of Python metaclass I - __getattr__ for static/class variable
2012-07-02 14:25 1320In my previous post, I disusses ... -
The module startup script
2012-06-27 12:00 0In my previous post. I discusse ... -
Recommended way to organize the Python project base
2012-06-26 18:24 340This article shows with example ... -
Python subprocess example1
2012-06-04 15:49 1050This article is based on the of ... -
Python Initialise a list to a specific length
2012-06-01 18:28 932Below show some tricks that y ... -
Python Format String Specification
2012-06-01 17:38 2601Since it is very common that yo ...
相关推荐
【作品名称】:泰迪杯 : 基于 python 实现 运输车辆安全驾驶行为的分析 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 在车辆运输过程中,不良驾驶行为主要包括疲劳驾驶、急加速、急减速、怠速预热、 超长怠速、熄火滑行、超速、急变道等。 针对以上运输车辆的不良驾驶行为,给出不同不良驾驶行为的判别标准,行车安全评价模型如下: 疲劳驾驶:连续行车时间超过4小时。 提取数据思路:若某一行acc_state列值为1并且gps_speed列数值大于0,则认为汽车开始启动,继续扫描数据表,直到寻找到一行gps_speed列的数值为0,则认为汽车已经处于停止状态,再根据location_time列由两个数据获取时间间隔,判断是否属于疲劳驾驶。 急加速、急减速:每两个经纬度间汽车的加速度达到或者超过20km/s^2。两个经纬度间汽车的加速 【资源声明】:本资源作为“参考资料”而不是“定制需求”,代码只能作为参考,不能完全复制照搬。需要有一定的基础看懂代码,自行调试代码并解决报错,能自行添加功能修改代码。
基于springboot的校园社交平台源码数据库文档.zip
scipy-1.7.1-cp37-cp37m-linux_armv7l.whl
java源码资源EJB 模拟银行ATM流程及操作源代码提取方式是百度网盘分享地址
pillow-11.0.0-cp39-cp39-linux_armv7l.whl
java面试视频资源微服务架构之Spring Cloud Eureka 场景分析与实战提取方式是百度网盘分享地址
基于springboot+vue的音乐播放系统源码数据库文档.zip
matplotlib-3.5.0-cp37-cp37m-linux_armv7l.whl
onnxruntime-1.16.2-cp311-cp311-win_amd64.whl
基于springboot复兴村医疗管理系统源码数据库文档.zip
环境说明: 开发软件:VS 2017 (版本2017以上即可,不能低于2017) 数据库:SqlServer2008r2(数据库版本无限制,都可以导入) 开发模式:mvc
onnxruntime-win-x64-gpu-1.19.2.zip
bimdata_api_client-4.0.7-py3-none-any.whl
基于springboot的实验室开放管理系统源码数据库文档.zip
Pillow-9.2.0-cp39-cp39-linux_armv7l.whl
STM32神舟III号例程源码STM32芯片按键点灯-无防抖(STM32神舟III号-寄存器版)提取方式是百度网盘分享地址
基于springboot医疗废物管理系统源码数据库文档.zip
基于springboot的车辆保险理赔平台源码数据库文档.zip