深刻理解Python中的元类(metaclass)(转)
转载地址:http://blog.jobbole.com/21351/
Stack Overflow上的好文:https://stackoverflow.com/questions/100003/what-are-metaclasses-in-python
另外有几点理解记录下:
创建一个实例时,有时会传入参数,这些参数会同时传入 __init__() 和 __new__(),如:
class Teacher(object):
def __init__(self, *args, **kwargs):
print("enter __init__")
print(args) def __new__(cls, *args, **kwargs):
print("enter __new__")
print(args)
return super(Teacher, cls).__new__(cls) wang = Teacher('xiaohong', 'xiaoming') 打印结果:
enter __new__
('xiaohong', 'xiaoming')
enter __init__
('xiaohong', 'xiaoming')
所以在如下代码中
class UpperAttrMetaclass(type):
def __new__(cls, future_class_name, future_class_parents, future_class_attr):
attr = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__'))
uppercase_attr = dict((name.upper(), value) for name, value in attr)
# return super(UpperAttrMetaclass, cls).__new__(cls, future_class_name, future_class_parents, uppercase_attr)
return type(future_class_name, future_class_parents, uppercase_attr) Foo2 = UpperAttrMetaclass('Foo2', (), {'bar': 'bip'}) print(Foo2.BAR) 结果:bip
Foo2 = UpperAttrMetaclass('Foo2', (), {'bar': 'bip'})实例化一个类对象时,三个参数实际也传给了 __new__ ,之前一直以为实例化对象时传进的参数只能是 __init__方法接收,
看来理解是不对的,所以使用type(future_class_name, future_class_parents, uppercase_attr)创建一个类时,三个参数也应该是传递给了type的__new__方法,这样才能通过__new__
方法使用三个实参构造一个类对象出来
深刻理解Python中的元类(metaclass)(转)的更多相关文章
- [转]深刻理解Python中的元类(metaclass)以及元类实现单例模式
使用元类 深刻理解Python中的元类(metaclass)以及元类实现单例模式 在看一些框架源代码的过程中碰到很多元类的实例,看起来很吃力很晦涩:在看python cookbook中关于元类创建单例 ...
- 深刻理解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)【转】
译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...
- 深刻理解Python中的元类(metaclass)以及元类实现单例模式
在理解元类之前,你需要先掌握Python中的类.Python中类的概念借鉴于Smalltalk,这显得有些奇特.在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.在Python中这一点仍 ...
- python——深刻理解Python中的元类(metaclass)
译注:这是一篇在Stack overflow上 很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉 ...
- 深刻理解Python中的元类(metaclass)--代码实践
根据http://blog.jobbole.com/21351/所作的代码实践. 这篇讲得不错,但以我现在的水平,用到的机会是很少的啦... #coding=utf-8 class ObjectCre ...
- 深入理解Python中的元类(metaclass)
原文 译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍 ...
随机推荐
- mac安装mysql及终端操作mysql与pycharm的数据库可视化
一.Mac安装mysql 首先下载mysql,地址:https://dev.mysql.com/downloads/mysql/ 然后已知安装就好了,会出现让你记住密码的提示,然后就安装好了.... ...
- Ubuntu14.04下redis安装 配置, redis主从配置
1.到官网下载redis源码包 wget http://download.redis.io/releases/redis-3.2.8.tar.gz 2.解压 并 编译 .tar.gz cd redis ...
- not in查询不出数据问题
select ID from SmartCustomer where ID not in (select distinct CustomerID from SmartPromoter where Cu ...
- 从零开始学习前端JAVASCRIPT — 9、JavaScript基础RegExp(正则表达式)
1:正则的概念 正则表达式(regular expression)是一个描述字符规则的对象.可以用来检查一个字符串是否含有某个子字符串,将匹配的子字符串做替换或者从某个字符串中取出符合某个条件的子串等 ...
- Linux学习系列——零基础开始
第一部分 Linux基础命令 1.查看系统信息命令 2.Linux内核版本 3.修改环境变量
- Linux查询系统信息命令
Linux查看系统信息是比较基础的知识,所以这个应该都需要掌握,命令和解释如下: #uname -a 查看操作系统.内核.CPU信息 #head -n 1 /etc/issue ...
- ScreenCapturePro2 for Joomla_3.4.7-tinymce4x
1.1. 与Joomla_3.4.7-tinymce4x整合 示例下载:Joomla_3.4.7, 1.1.1. 添加screencapture文件夹 1.1.2. 2.添加插件文件夹 路径: ...
- Logback configuration
官方指导 http://logback.qos.ch/manual/configuration.html 规则 ch.qos.logback.core.joran.JoranConfiguratorB ...
- TangoAreaDescriptionMetaData区域描述元数据
TangoAreaDescriptionMetaData com.google.atap.tangoservice Class TangoAreaDescriptionMetaData java.la ...
- .net 可选参数和命名参数
可选参数,可以为方法的参数设置默认值,在调用方法的时候,如果不传递参数,就是执行默认参数. class Program { , string s = "A", DateTime d ...