Mateclass

一切皆对象:

Eg:

class Foo:

pass

f=Foo()

In [60]: print(type(f))

<class '__main__.Foo'>

In [61]: print(type(Foo))

<class 'type'>

即: f是Foo的对象 Foo是type的对象

那么我们现在不用type作为Foo的类:

  1. # coding=utf-8  
  2. class Mytype(type):  
  3.     def __init__(cls, what, bases=None, dict=None):  
  4.         super().__init__(what, bases, dict)  
  5.         print("Mytype_init_")  
  6.     
  7.     def __call__(self, *args, **kwargs):
  8.          return super().__call__(*args, **kwargs)
  9.     
  10.     
  11. class Foo(metaclass=Mytype):  
  12.     def __init__(self):  
  13.         super().__init__()  
  14.         print("Foo__init__")  
  15.    
  16.     @staticmethod  
  17.     def __new__(cls, *more):  
  18.         print("Foo__new__")  
  19.         # return super().__new__(cls, *more)  

解释器从上到下执行,9行之前,先由type创建行,创建Foo对象,由type的__call__方法调用Mytype的__new__方法,再是__init__方法;

这时候执行,可以看到的运行结果为: Mytype_init_

下面继续:

  1. # coding=utf-8  
  2. class Mytype(type):  
  3.     def __init__(cls, what, bases=None, dict=None):  
  4.         super().__init__(what, bases, dict)  
  5.         print("Mytype_init_")  
  6.     
  7.     def __call__(self, *args, **kwargs):  
  8.         print("Mytype____call__")  
  9.         print(self.__name__)  #Foo
  10.         print(self)  #<class '__main__.Foo'>
  11.         # return super().__call__(*args, **kwargs)  
  12.     
  13.     
  14.     
  15. class Foo(metaclass=Mytype):  
  16.     def __init__(self):  
  17.         super().__init__()  
  18.         print("Foo__init__")  
  19.    
  20.     @staticmethod  
  21.     def __new__(cls, *more):  
  22.         print("Foo__new__")  
  23.         # return super().__new__(cls, *more)  
  24.     
  25. f=Foo() 

可以看到在25行的时候后面加括号,调用了Foo对象的__call__方法,这时候self就是Foo,在这里调用self(Foo)的__new__方法,

接下来

  1. # coding=utf-8  
  2. class Mytype(type):  
  3.     def __init__(cls, what, bases=None, dict=None):  
  4.         super().__init__(what, bases, dict)  
  5.         print("Mytype_init_")  
  6.     
  7.     def __call__(self, *args, **kwargs):  
  8.         print("Mytype____call__")  
  9.         return super().__call__(*args, **kwargs)  
  10.     
  11.     
  12.     
  13. class Foo(metaclass=Mytype):  
  14.     def __init__(self):  
  15.         super().__init__()  
  16.         print("Foo__init__")  
  17.    
  18.     @staticmethod  
  19.     def __new__(cls, *more):  
  20.         print("Foo__new__")  
  21.         print(super().__new__(cls, *more))   #<__main__.Foo object at 0x01F2BAD0>
  22.         return super().__new__(cls, *more)  
  23.     
  24. f=Foo()  
  25.     
  26. # Mytype_init_  
  27. # Mytype____call__  
  28. # Foo__new__  
  29. # <__main__.Foo object at 0x01F2BAD0>  
  30. # Foo__init__  
  31. #  
  32. # Process finished with exit code  0  

So 当我们调用父类的方法时,顺带打印下我们关系的运行关系:

可以看到,object对象是在__new__方法中生成的.

总结:实例化一个类: 先在定义类的时候,解释器已经对其进行解释,type类执行,实例化的时候type类的__call__方法被调用,type.__call__方法将调用该类的__new__方法.此时对象生成,然后该类的__init__方法被调用.

即:实例化一个类,他的__new__方法先被执行,并在此时创建对象,然后,__init__方法被执行

Mateclass的更多相关文章

  1. 5、flask之信号和mateclass元类

    本篇导航: flask实例化参数 信号 metaclass元类解析 一.flask实例化参数 instance_path和instance_relative_config是配合来用的:这两个参数是用来 ...

  2. flask之信号和mateclass元类

    本篇导航: flask实例化参数 信号 metaclass元类解析 一.flask实例化参数 instance_path和instance_relative_config是配合来用的:这两个参数是用来 ...

  3. 4、flask之分页插件的使用、添加后保留原url搜索条件、单例模式

    本篇导航: flask实现分页 添加后保留原url搜索条件 单例模式 一.flask实现分页 1.django项目中写过的分页组件 from urllib.parse import urlencode ...

  4. python基础——面向对象进阶下

    python基础--面向对象进阶下 1 __setitem__,__getitem,__delitem__ 把对象操作属性模拟成字典的格式 想对比__getattr__(), __setattr__( ...

  5. python中的类机制

    一.python中的对象 1.python中对象种类及关系 <type 'type'>:该对象可以成为其他类的类型,python中几乎所有对象都是直接或间接由<type 'type' ...

  6. python之序列化模块、双下方法(dict call new del len eq hash)和单例模式

    摘要:__new__ __del__ __call__ __len__ __eq__ __hash__ import json 序列化模块 import pickle 序列化模块 补充: 现在我们都应 ...

  7. flask之分页插件的使用、添加后保留原url搜索条件、单例模式

    本篇导航: flask实现分页 添加后保留原url搜索条件 单例模式 一.flask实现分页 1.django项目中写过的分页组件 from urllib.parse import urlencode ...

  8. 面向对象之—property,staticmethod

    一 特性( property) property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值. property是内置的一种封装方法:把一个属性“伪装”成一个数据属性,做法就是在需要伪装 ...

  9. 【OC底层】OC对象本质,如 isa, super-class

    Objective-C的本质 1.我们编写的Objective-C,底层现实都是C/C++,代码生成步骤如下:   2.在OC中的所有面向对象的实现,都是基于C/C++的数据结构实现的 3.将Obje ...

随机推荐

  1. mysql安装出现 conflicts with mysql*的解决办法

    rpm -ivh Percona-Server-client-56-5.6.16-rel64.0.el6.x86_64.rpm --nodeps --force error: Failed depen ...

  2. 关于Android Studio更新后一直Refreshing的解决办法!

    今天更新了一下studio一直是这个问题 查了很多资料终于解决了 造成这个问题的原因是要更新的gradle版本和studio安装路径中的gradle版本不一致导致的 把他们改成一致的即可 在这个目录里 ...

  3. 深入分析C++虚函数表

    C++中的虚函数(Virtual Function)是用来实现动态多态性的,指的是当基类指针指向其派生类实例时,可以用基类指针调用派生类中的成员函数.如果基类指针指向不同的派生类,则它调用同一个函数就 ...

  4. ajax第二天学习

    post方式发送请求 要首先设置请求头(参数设置为ajax.setRequestHeader("content-type","application/x-www-form ...

  5. 初见UDP_Server

    from socket import *ip_prot = ('192.168.55.1',8080)buffer_size = 1024udp_sever = socket(AF_INET,SOCK ...

  6. BZOJ 3203 [SDOI2013]保护出题人 (凸包+三分)

    洛谷传送门 题目大意:太长略 每新加入一个僵尸,容易得到方程$ans[i]=max{\frac{sum_{i}-sum_{j-1}}{s_{i}+d(i-j)}}$ 即从头开始每一段僵尸都需要在规定距 ...

  7. TI低功耗蓝牙(BLE)介绍

    TI低功耗蓝牙(BLE)介绍 本文档翻译和修改自参考资料:CC2540Bluetooth Low Energy Software Developer’s Guide (Rev. B),部分图片直接引用 ...

  8. java中 flush()方法的作用

    flush() 是清空,而不是刷新啊.一般主要用在IO中,即清空缓冲区数据,就是说你用读写流的时候,其实数据是先被读到了内存中,然后用数据写到文件中,当你数据读完的时候不代表你的数据已经写完了,因为还 ...

  9. WPF中多线程统计拆箱装箱和泛型的运行效率

    WPF中多线程统计拆箱装箱和泛型的执行效率.使用的知识点有泛型.多线程.托付.从样例中能够看到使用泛型的效率至少提升2倍 MainWindow.xaml <Window x:Class=&quo ...

  10. C++开发人脸性别识别教程(7)——搭建MFC框架之界面绘制

    在之前的博客中我们已经将项目中用到的算法表述完成,包含人脸检測算法以及四种性别识别算法,在这篇博客中我们将着手搭建主要的MFC框架. 一.框架概况 在这篇博文中我们将搭建最主要的MFC框架.绘制MFC ...