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)
  
= 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__ shuaigaogao
Foo __new__ <class '__main__.Foo'>
obj  <__main__.Foo object at 0x0000025EF0F05048> shuaigaogao
<class '__main__.Foo'>
Foo __init__
f <__main__.Foo object at 0x0000025EF0F05048>
fname shuaigaogao

创建过程如下:

更多__metaclass__知识:点击

总结:

  类的生成 调用 顺序依次是 __new__ --> __init__ --> __call__

__metaclass__方法的更多相关文章

  1. Python全栈开发之7、面向对象编程进阶-类属性和方法、异常处理和反射

    一.类的属性 1.@property属性 作用就是通过@property把一个方法变成一个静态属性 class Room: def __init__(self,name,length,width,he ...

  2. python高级编程之元类(第3部分结束)

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #元编程 #new-style类带来了一种能力,通过2个特殊方法(_ ...

  3. python中Metaclass的理解

    今天在学习<python3爬虫开发实战>中看到这样一段代码3 class ProxyMetaclass(type): def __new__(cls, name, bases, attrs ...

  4. 面向对象【day08】:类的起源与metaclass(二)

    本节内容 1.概述 2.类的起源 3.__new__方法 4.__metaclass__方法 一.概述 前面我们学习了大篇幅的关于类,通过类创建对象,那我们想知道这个类到底是怎么产生的呢?它的一切来源 ...

  5. python type metaclass

    在python中一切皆对象, 所有类的鼻祖都是type, 也就是所有类都是通过type来创建. 传统创建类 class Foo(object): def __init__(self,name): se ...

  6. 类的起源与metaclass

    一.概述 我们知道类可以实例化出对象,那么类本身又是怎么产生的呢?我们就来追溯一下类的起源. 二.类的起源 2.1 创建一个类 class Foo(object): def __init__(self ...

  7. 【python】-- 类的创建、__new__、__metaclass___

    类的创建 前面的随笔都是关于类的知识,通过类创建对象,那这个类到底是怎么产生的呢? 1. 传统创建类 class Foo(object): def __init__(self,name): self. ...

  8. 谈谈Python中元类Metaclass(一):什么是元类

    简单的讲,元类创建了Python中所有的对象. 我们说Python是一种动态语言,而动态语言和静态语言最大的不同,就是函数和类不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个HelloW ...

  9. Python基础—面向对象(进阶篇)

    通过上一篇博客我们已经对面向对象有所了解,下面我们先回顾一下上篇文章介绍的内容: 上篇博客地址:http://www.cnblogs.com/phennry/p/5606718.html 面向对象是一 ...

随机推荐

  1. Spring——代理工厂实现增强

    借助Spring IOC的机制,为ProxyFactory代理工厂的属性实现依赖注入,这样做的优点是可配置型高,易用性好. 1.创建抽象主题 public interface ProService { ...

  2. MySQL基础入门之常用命令使用

    如何启动MySql服务 /etc/init.d/mysqld start service mysqld start Centos .x 系统 sysctl start mysqld 检测端口是否运行 ...

  3. AtCoder AGC038F Two Permutations (网络流、最小割)

    题目链接 https://atcoder.jp/contests/agc038/tasks/agc038_f 题解 好题. 首先观察到一个性质,对于排列\(P\), 其所形成的每个轮换中的点\(A_i ...

  4. MySQL_(Java)使用JDBC创建用户名和密码校验查询方法

    MySQL_(Java)使用JDBC向数据库发起查询请求 传送门 MySQL数据库中的数据,数据库名garysql,表名garytb,数据库中存在的用户表 通过JDBC对MySQL中的数据用户名和密码 ...

  5. font属性

    font属性 font属性设置css字体

  6. webservice代码编写主要包括服务器端发布和客户端调用。

    一.java工程发布,java工程调用   (一).服务器端的编写 1.在eclipse里新建java project工程,创建功能类,通过关键字@webservice(name="newI ...

  7. truncate at 255 characters with xlsx files(OLEDB方式读取Excel丢失数据、字符串截断的原因和解决方法)

    The TypeGuessRows setting is supported by ACE. Note the version numbers in the key may change depend ...

  8. NetScaler VPX configration

    境搭建概述 本文主要介绍Netscaler的安装配置,以及与StoreFront相结合,实现外网访问内网资源.当用户访问时,Netscaler Gateway Virtual Server将把请求转给 ...

  9. Anaconda 改为国内镜像的方法

    Anaconda的conda 特别好用 但如果用国外的镜像,慢的出奇 可以改为了国内镜像会好很多 conda config --add channels https://mirrors.tuna.ts ...

  10. 一些有意思的git

    fs: https://github.com/psankar/simplefs https://github.com/gzc/isystem/blob/master/basic/Crash_Consi ...