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. Apache2.2伪静态配置

    最近由于工作的需要要配置一下Apache的伪静态化,在网上搜了好多都无法完成,所以觉得有必要在这里写一下. 第一步:打开Apache的httpd.conf文件,把LoadModule rewrite_ ...

  2. Codeforces Round #283 (Div. 2) A

    解题思路:给出一个递增数列,a1,a2,a3,-----,an.问任意去掉a2到a3之间任意一个数之后, 因为注意到该数列是单调递增的,所以可以先求出原数列相邻两项的差值的最大值max, 得到新的一个 ...

  3. DHCPv6,IPv6的有状态自动配置

    DHCPv6,IPv6的有状态自动配置 DHCPv6的工作原理与DHCPv4极其相似,但有一个明显的差别,那就是支持IPV6新增的编址方案.DHCP提供了一些自动配置没有的选项.在自动配置中,根本没有 ...

  4. springboot中文文档

    http://blog.geekidentity.com/spring/spring_boot_translation/ https://blog.csdn.net/zfrong/article/de ...

  5. 为Electron 安装 vue-devtool等扩展

    相关代码: https://github.com/WozHuang/Barrage-helper/blob/master/src/main/index.dev.js 在SPA逐渐成为构建优秀交互体验应 ...

  6. HDU 6149 Valley Numer II (状压DP 易错题)

    题目大意:给你一个无向连通图(n<=30),点分为高点和低点,高点数量<=15,如果两个高点和低点都直接连边,那么我们称这三个点形成一个valley,每个点最多作为一个valley的组成部 ...

  7. 自动合法打印VitalSource Bookshelf中的电子书

    最近有一本2千多页的在VitalSource中的电子书想转为PDF随时阅读,没料网上找了一圈没有找到合适的.相对好一些的只有一个用Python写的模拟手动打印.于是想到了用AutoHotkey写一个自 ...

  8. Jquery学习总结(3)——Jquery获取当前城市的天气信

    Jquery代码: function findWeather() {     var cityUrl = 'http://int.dpool.sina.com.cn/iplookup/iplookup ...

  9. [CSS3] Create Dynamic Styles with CSS Variables

    In this lesson we are going to use CSS variables to keep our application's colors consistent. This i ...

  10. what&#39;s new in vc2015

    1. 变量和函数的注解提示非常实用.象C#了. 2.CStdioFile升级了,不再须要象 vc2013中,用CStdioFileEx来修复错误了. 3. 发现再写.