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. 安装django和selenium

    安装很简单(前提是python已经安装),命令窗口直接输入pip install django,回车就会自动进行安装,selenium也是一样pip install selenium 启动django ...

  2. Windows系统Python环境搭建

    Python下载 下载地址:https://www.python.org/downloads/ 选择需要下载的版本 以Python3.3.7版本为例,下载64位和32位都分别有三种方式,依次是压缩包, ...

  3. 使用C++部署Keras或TensorFlow模型

    本文介绍如何在C++环境中部署Keras或TensorFlow模型. 一.对于Keras, 第一步,使用Keras搭建.训练.保存模型. model.save('./your_keras_model. ...

  4. virtualenv和virtualenvwrapper的安装与使用

    环境 Windows 10 python 3.6.7 virtualenv 安装 virtualenv用于创建虚拟环境,用于隔离不同的python版本的运行,是容器类软件.这里在Windows下通过p ...

  5. javaScript(其他引用类型对象)

    javascript其他引用类型对象 Global对象(全局)这个对象不存在,无形的对象,无法new一个 其内部定义了一些方法和属性:如下 encodeURI str = www.baidu.com ...

  6. UVALive-8077 Brick Walls 找规律

    题目链接:https://cn.vjudge.net/problem/UVALive-8077 题意 有一个用砖头磊起来的墙,现在又有一只蚂蚁,想沿着砖缝从起点跑到终点. 问最短路长度. 思路 找规律 ...

  7. Redis字符串(STRING)中BIT相关命令

    上篇文章我们对STRING数据类型中一些基本的命令进行了介绍,但是没有涉及到BIT相关的命令,本文我们就来看看几个和BIT相关的命令. 本文是Redis系列的第四篇文章,了解前面的文章有助于更好的理解 ...

  8. javascript 中一些奇葩的日期换算

    1.获取今天的0时0分0秒(常用于开始日期的获取) new Date(new Date().toLocaleDateString()); // Mon Nov 12 2018 00:00:00 GMT ...

  9. JS中的DOM操作怎样添加、移除、移动、复制、创建和查找节点

    DOM操作怎样添加.移除.移动.复制.创建和查找节点? (1)创建新节点 createDocumentFragment() //创建一个DOM片段 createElement() //创建一个具体的元 ...

  10. Run Nutch In Eclipse on Linux and Windows nutch version 0.9

    Running Nutch in Eclipse Here are instructions for setting up a development environment for Nutch un ...