__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__ shuaigaogaoFoo __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 面向对象是一 ...
随机推荐
- URAL 2036 Intersect Until You're Sick of It 形成点的个数 next_permutation()函数
A - Intersect Until You're Sick of It Time Limit:500MS Memory Limit:65536KB 64bit IO Format: ...
- vue 甘特图简单制作
甘特图(Gantt chart)又称为横道图.条状图(Bar chart).其通过条状图来显示项目,进度,和其他时间相关的系统进展的内在关系随着时间进展的情况.以提出者亨利·L·甘特(Henrry L ...
- Keras学习笔记三:一个图像去噪训练并离线测试的例子,基于mnist
训练模型需要的数据文件有: MNIST_data文件夹下的mnist_train.mnist_test.noisy_train.noisy_test.train文件夹下60000个图片,test下10 ...
- BAT 鼎立格局被打破,2019 年这些互联网公司是程序员跳槽首选!
点击上方“程序员江湖”,选择“置顶或者星标” 你关注的就是我关心的! 作者:BOSS直聘 来源:BOSS直聘 作者:BOSS直聘(ID:bosszhipin),领先的移动互联网招聘APP,为求职者 ...
- OUC_Summer Training_ DIV2_#5
这是做的最好的一次了一共做了4道题 嘻嘻~ A - Game Outcome Time Limit:2000MS Memory Limit:262144KB 64bit IO For ...
- Python 之类与对象及继承
类与对象 学习类的语法 关键字 class 类别,分类class 类名:属性特性特征类名的编写规范:首字母大写 驼峰命令 见名知意--->遵守规范.Math StudentInfoclass S ...
- ubuntu下tomcat运行不起来解决
报错Neither the JAVA_HOME nor the JRE_HOME environment variable is definedAt least one of these enviro ...
- javascript之Location对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 通过ssh管道连接内网数据库(mysql)
公网连接内网数据库(如云数据库)时,通常需要白名单:如果不是白名单IP,通常需要一个跳板机(类似代理)来连接内网数据库, 下方以mysql为例(其他数据库基本一致): import pymysql a ...
- docker命令大全与资源汇总
容器生命周期管理 run //创建一个新的容器并运行一个命令 start/stop/restart //启动一个或多个已经被停止的容器:停止一个运行中的容器:重启容器 kill //杀掉一个运行 ...