Django中的元类-乾颐堂
看Django(1.6)的Form相关源代码时比较迷惑,于是节选了django.forms.forms.py中的几个代码片段来分析Django中是怎么使用元类的:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
def with_metaclass(meta, *bases): """Create a base class with a metaclass.""" return meta("NewBase", bases, {}) class DeclarativeFieldsMetaclass(type): def __new__(cls, name, bases, attrs): print('cls: %s, name: %s, bases: %s ,attrs: %s\n' % (cls, name, bases, attrs)) new_class = super(DeclarativeFieldsMetaclass, cls).__new__(cls, name, bases, attrs) # new_class._meta = '123' return new_class class BaseForm(object): pass class Form(with_metaclass(DeclarativeFieldsMetaclass, BaseForm)): pass class MyForm(Form): a = 1 b = 2 |
加载上面的Python模块,控制台会输出:
cls: <class '__main__.DeclarativeFieldsMetaclass'>, name: NewBase, bases: (<class '__main__.BaseForm'>,), attrs: {}
cls: <class '__main__.DeclarativeFieldsMetaclass'>, name: Form, bases: (<class '__main__.NewBase'>,), attrs: {'__module__': '__main__'}
cls: <class '__main__.DeclarativeFieldsMetaclass'>, name: MyForm, bases: (<class '__main__.Form'>,), attrs: {'a': 1, '__module__': '__main__', 'b': 2}
虽然没有直接在代码中为MyForm指定metaclass,但由于MyForm继承自Form,而Form继承自DeclarativeFieldsMetaclass生成的类名为"NewBase"的类,所以DeclarativeFieldsMetaclass实际上就是MyForm的metaclass。
http://www.qytang.com/cn/list/28/393.htm
http://www.qytang.com/cn/list/28/391.htm
http://www.qytang.com/cn/list/28/389.htm
http://www.qytang.com/cn/list/28/388.htm
http://www.qytang.com/cn/list/28/362.htm
http://www.qytang.com/cn/list/28/358.htm
http://www.qytang.com/cn/list/28/351.htm
http://www.qytang.com/cn/list/28/348.htm
http://www.qytang.com/cn/list/28/340.htm
http://www.qytang.com/cn/list/28/338.htm
http://www.qytang.com/cn/list/28/336.htm
http://www.qytang.com/cn/list/28/330.htm
Django中的元类-乾颐堂的更多相关文章
- python2中的__new__与__init__,新式类和经典类-乾颐堂
在python2.x中,从object继承得来的类称为新式类(如class A(object))不从object继承得来的类称为经典类(如class A()) 新式类跟经典类的差别主要是以下几点: 1 ...
- Django常见出错解决方案汇总-乾颐堂
一.模板类型错误: 错误原因:在models中漏掉了return: class UserProfile(models.Model): """ 功能说明: 扩 ...
- 注意for循环中变量的作用域-乾颐堂
1 2 for e in collections: pass 在for 循环里, 最后一个对象e一直存在在上下文中.就是在循环外面,接下来对e的引用仍然有效. 这里有个问题容易被忽略,如果在循 ...
- 深刻理解Python中的元类metaclass(转)
本文由 伯乐在线 - bigship 翻译 英文出处:stackoverflow 译文:http://blog.jobbole.com/21351/ 译注:这是一篇在Stack overflow上很热 ...
- 深刻理解Python中的元类(metaclass)
译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...
- [转] 深刻理解Python中的元类(metaclass)
非常详细的一篇深入讲解Python中metaclass的文章,感谢伯乐在线-bigship翻译及作者,转载收藏. 本文由 伯乐在线 - bigship 翻译.未经许可,禁止转载!英文出处:stacko ...
- [转]深刻理解Python中的元类(metaclass)以及元类实现单例模式
使用元类 深刻理解Python中的元类(metaclass)以及元类实现单例模式 在看一些框架源代码的过程中碰到很多元类的实例,看起来很吃力很晦涩:在看python cookbook中关于元类创建单例 ...
- 深刻理解Python中的元类(metaclass)【转】
译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...
- 深刻理解Python中的元类(metaclass)以及元类实现单例模式
在理解元类之前,你需要先掌握Python中的类.Python中类的概念借鉴于Smalltalk,这显得有些奇特.在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.在Python中这一点仍 ...
随机推荐
- 批量修改文件名的bash脚本
#!/bin/bash while IFS='' read -r line || [[ -n "$line" ]]; do # echo "sox $line --cha ...
- 分布式事务之:TCC (Try-Confirm-Cancel) 模式
在当前如火如荼的互联网浪潮下,如何应对海量数据.高并发成为大家面临的普遍难题.广大IT公司从以往的集中式网站架构,纷纷转向分布式的网站架构,随之而来的就是进行数据库拆分和应用拆分,如何在跨数据库.跨应 ...
- [转][Java]语法规范
先来个 阿里的 Java 开发手册(2017.5.20 发布 v1.2.0 .pdf) 本文来自:http://blog.csdn.net/sinat_27115575/article/details ...
- CentOS6.4安装辅助NIS的流程
服务器端软件包安装 yum -y install yp-tools ypbind ypserv rpcbind 设置NIS的域名 echo 'NISDOMAIN=liebaonis.local' &g ...
- 渗透辅助神器 - DZGEN
项目地址:https://github.com/joker25000/DZGEN git clone ┌─[root@sch01ar]─[/sch01ar] └──╼ #git clone https ...
- 基于RabbitMQ的跨平台RPC框架
RabbitMQRpc protocobuf RabbitMQ 实现RPC https://www.cnblogs.com/LiangSW/p/6216537.html 基于RabbitMQ的RPC ...
- ASU一位图形学老师推荐的book list
http://peterwonka.net/Documentation/BooksToRead.htm 包括数学.图形学.OpenGL等资料
- 如何清除svn的账号缓存信息(solaris)
如果我们不小心输入svn账号错误的话,后面就一直提示认证失败,不能checkout代码. 这个是因为svn把你输入的账号进行了缓存. 如果我们想重新输入新的账号,必须要清除缓存 svn存储账号的目录在 ...
- sql开启xp_cmdshell
sql开启xp_cmdshell sp_configure reconfigure go sp_configure reconfigure go
- GetEnumName 枚举名称 字符串
System.TypInfo.pas System.TypInfo.hpp http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.TypIn ...