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. java根据出生日期计算年龄

    /** * @author jerry.chen * @param brithday * @return * @throws ParseException * 根据生日获取年龄; */ public ...

  2. Apache2.2伪静态配置

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

  3. 再生龙恢复分区后修复引导或debian linux修复引导 三部曲

    先参考 sudo -imkdir /mntmount /dev/sda1 /mntgrub-install --force --no-floppy --root-directory=/mnt /dev ...

  4. 问题请教:关于同一个POD中多容器的广播信息问题

    广大博友好,最近在K8S集群中遇到一个问题,贴出来同大家分享一下 同一个POD中多个容器 如何处理广播信息? 经测试 同一个POD中当先启动的容器占用广播端口后,其他的容器启动就会报bind erro ...

  5. php 文件加载方式

    两种加载文件的方式 include require 使用场景: 动态加载文件的时候,使用include,否则使用require. 示例: # 引入php文件--include方式 inlcude(&q ...

  6. 小学生都能学会的python(函数)

    小学生都能学会的python(函数) 神马是函数 函数: 对功能或者动作的封装 函数的定义 def 函数名(形参列表): 函数体(return) ret = 函数名(实参列表) 函数的返回值 retu ...

  7. 模块打包机--webpack--基础使用

    什么是webpack? 作用有哪些?   WebPack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,Type ...

  8. shiro + maven 的web配置(不整合spring)

    本文采用的是1.4.0版本的shiro 官方中说的1.2之前,和之后的shiro配置分别为: 1.2之前: <filter> <filter-name>iniShiroFilt ...

  9. java开发必背API

    1.java.io.file类,File用于管理文件或目录: 所属套件:java.io File file = new File(fileStringPath); 1)file.mk(),真的会创建一 ...

  10. [CSS3] CSS Background Images

    Body with background image and gradient html { background: linear-gradient(#000, white) no-repeat; h ...