__metaclass__方法
metaclass这个属性叫做元类,它是用来表示这个类是由谁来帮他实例化创建的,说白了,就是相当于自己定制一个类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
class MyType( type ): def __init__( self , * args, * * kwargs): print ( "Mytype __init__" , * args, * * kwargs) def __call__( self , * args, * * kwargs): print ( "Mytype __call__" , * args, * * kwargs) obj = self .__new__( self ) print ( "obj " ,obj, * args, * * kwargs) print ( self ) self .__init__(obj, * args, * * kwargs) return obj def __new__( cls , * args, * * kwargs): print ( "Mytype __new__" , * args, * * kwargs) return type .__new__( cls , * args, * * kwargs) class Foo( object ,metaclass = MyType): #python3统一用这种 #__metaclass__ = MyType #python2.7中的写法 def __init__( self ,name): self .name = name print ( "Foo __init__" ) def __new__( cls , * args, * * kwargs): print ( "Foo __new__" , cls , * args, * * kwargs) return object .__new__( cls ) f = Foo( "shuaigaogao" ) print ( "f" ,f) print ( "fname" ,f.name) #输出 Mytype __new__ Foo (< class 'object' >,) { '__new__' : <function Foo.__new__ at 0x0000025EF0EFD6A8 >, '__init__' : <function Foo.__init__ at 0x0000025EF0EFD620 >, '__qualname__' : 'Foo' , '__module__' : '__main__' } Mytype __init__ Foo (< class 'object' >,) { '__new__' : <function Foo.__new__ at 0x0000025EF0EFD6A8 >, '__init__' : <function Foo.__init__ at 0x0000025EF0EFD620 >, '__qualname__' : 'Foo' , '__module__' : '__main__' } Mytype __call__ shuaigaogao Foo __new__ < class '__main__.Foo' > obj <__main__.Foo object at 0x0000025EF0F05048 > shuaigaogao < class '__main__.Foo' > Foo __init__ f <__main__.Foo object at 0x0000025EF0F05048 > fname shuaigaogao |
创建过程如下:
总结:
类的生成 调用 顺序依次是 __new__ --> __init__ --> __call__
__metaclass__方法的更多相关文章
- Python全栈开发之7、面向对象编程进阶-类属性和方法、异常处理和反射
一.类的属性 1.@property属性 作用就是通过@property把一个方法变成一个静态属性 class Room: def __init__(self,name,length,width,he ...
- python高级编程之元类(第3部分结束)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #元编程 #new-style类带来了一种能力,通过2个特殊方法(_ ...
- python中Metaclass的理解
今天在学习<python3爬虫开发实战>中看到这样一段代码3 class ProxyMetaclass(type): def __new__(cls, name, bases, attrs ...
- 面向对象【day08】:类的起源与metaclass(二)
本节内容 1.概述 2.类的起源 3.__new__方法 4.__metaclass__方法 一.概述 前面我们学习了大篇幅的关于类,通过类创建对象,那我们想知道这个类到底是怎么产生的呢?它的一切来源 ...
- python type metaclass
在python中一切皆对象, 所有类的鼻祖都是type, 也就是所有类都是通过type来创建. 传统创建类 class Foo(object): def __init__(self,name): se ...
- 类的起源与metaclass
一.概述 我们知道类可以实例化出对象,那么类本身又是怎么产生的呢?我们就来追溯一下类的起源. 二.类的起源 2.1 创建一个类 class Foo(object): def __init__(self ...
- 【python】-- 类的创建、__new__、__metaclass___
类的创建 前面的随笔都是关于类的知识,通过类创建对象,那这个类到底是怎么产生的呢? 1. 传统创建类 class Foo(object): def __init__(self,name): self. ...
- 谈谈Python中元类Metaclass(一):什么是元类
简单的讲,元类创建了Python中所有的对象. 我们说Python是一种动态语言,而动态语言和静态语言最大的不同,就是函数和类不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个HelloW ...
- Python基础—面向对象(进阶篇)
通过上一篇博客我们已经对面向对象有所了解,下面我们先回顾一下上篇文章介绍的内容: 上篇博客地址:http://www.cnblogs.com/phennry/p/5606718.html 面向对象是一 ...
随机推荐
- delphi请求http接口中文乱码问题
请求http接口的时候参数值是中文乱码: http接口一般都是由java,php以及C#开发而成的,乱码的原因也是由于编码的问题,一般传递数据的都是utf8,然后传递的时候都会urlEcode 那么d ...
- 修改hive 默认fs为s3 遇到的坑
问题: hive我修改了 默认的f <property> <name>fs.defaultFS</name> <value>hdfs://...:802 ...
- Python代码整洁之道(一)
很多新手在开始学一门新的语言的时候,往往会忽视一些不应该忽视的细节,比如变量命名和函数命名以及注释等一些内容的规范性,久而久之养成了一种习惯.对此呢,我特意收集了一些适合所有学习 Python 的人, ...
- Nginx-rtmp之监听端口的管理
1. 概述 监听端口属于 server 虚拟主机,它是由 server{} 块下的 listen 配置项决定的. 每监听一个 TCP 端口,都将使用一个独立的 ngx_rtmp_conf_port_t ...
- 【Blog怎么玩】什么叫EntryName友好地址名?
EntryName友好地址名 其实就是你这个页面的别名,如果设置的好的话,在SEO搜索中,会更清晰的显示出来. 好处1:清晰的URL 比如我有一篇展示可爱兔子的博文,我想让它的网址清晰的显示出来,而不 ...
- 石川es6课程---4、箭头函数
石川es6课程---4.箭头函数 一.总结 一句话总结: 相当于函数的简写,类似python lambda 函数,先了解即可 let show1 = function () { console.log ...
- leetcode-hard-ListNode-23. Merge k Sorted Lists
mycode 91.2% # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x ...
- mysql 查询锁表,解锁
//1.查看当前数据库锁表的情况 SELECT * FROM information_schema.INNODB_TRX; //2.杀掉查询结果中锁表的trx_mysql_thread_id ...
- Java IO & Serialization
Java IO & Serialization 专为开卷考试准备,内容包括基本的文本文件和二进制文件的读写以及序列化反序列化操作 IO demo package helloworld; imp ...
- FinalShell Mac OS版,Linux版安装及教程(Mac下的xshell)
用户QQ群 342045988 Mac版安装路径/Applications/finalshelldata Linux版安装路径/usr/lib/finalshelldata 注意:1.FinalShe ...