1. print( 坑的信息 )

  • 挖坑时间:2019/04/07
  • 明细
坑的编码 内容
Py024-2 MethodType 举例
Py024-3 type 举例
Py024-4 MetaClass 举例

2. 开始填坑

2.1 MetaClass 举例

from types import MethodType

class A():
pass def say(self):
print("this is say()") a = A()
a.say = MethodType(say, A)
a.say()

>>>

this is say()

2.2 type 举例

# 1. 定义类应该具有的成员函数
def say(self):
print("this is say()") def eat(self):
print("this is eat()") # 2. 用 type 来创建一个类
A = type( "AName", (object, ), {"class_say":say, "class_eat":eat}) # 3. 像正常访问一样使用类
a = A() a.class_say()
a.class_eat()

>>>

this is say()

this is eat()

2.3 MetaClass 举例

# 元类写法是固定的,必须继承自 type

# 元类一般命名以 MetaClass 结尾
class ExampleMetaClass(type):
def __new__(cls, region, gender, attrs):
print("this is MetaClass")
attrs['id_num'] = '9527'
attrs['age'] = "18"
return type.__new__(cls, region, gender, attrs) # 元类定义完就可以使用
class Person(object, metaclass=ExampleMetaClass):
pass york = Person()
print(york.id_num)

>>>

this is MetaClass

9527

[Python3 填坑] 018 组装类的几个例子的更多相关文章

  1. [Python3 填坑] 006 “杠零”,空字符的使用

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...

  2. [Python3 填坑] 014 类的常用魔术方法举例

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 __init__() 2.2 __new__() 2.3 __call__() 2.4 __str__() 2.5 __repr__() ...

  3. [Python3 填坑] 013 几个类相关函数的举例

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 issubclass() 2.2 isinstance() 2.3 hasattr() 2.4 getattr() 2.5 setatt ...

  4. [Python3 填坑] 009 深拷贝与浅拷贝

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...

  5. [Python3 填坑] 004 关于八进制

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...

  6. [Python3 填坑] 017 实例方法、静态方法、类方法的区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 先上例子 2.2 分析 1. print( 坑的信息 ) 挖坑时间:2019/04/07 明细 坑的编码 内容 Py024-1 实例方法 ...

  7. [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...

  8. [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...

  9. [Python3 填坑] 005 如何“响铃”

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 1. print( 坑的信息 ) 挖坑时间:2019/01/08 明细 坑的编码 内容 Py004-2 ...

随机推荐

  1. [NOI2018]归程(kruscal重构树)

    [NOI2018]归程 题面太长辣,戳这里 模拟赛上写了一个spfa (关于spfa,它已经死了),然后一个st表水完暴力跑路.考后说是Kruscal重构树或者可持久化并查集???这都是些什么东西.不 ...

  2. classloader加载class的流程及自定义ClassLoader

    java应用环境中不同的class分别由不同的ClassLoader负责加载.一个jvm中默认的classloader有Bootstrap ClassLoader.Extension ClassLoa ...

  3. Jion的应用

  4. metaclass简单使用

    metaclass : 元类, 就是创建python类对象的类 # 便用type动态创建类 # 1. 使用type创建一个User类对象 无基类,无属性 . () : 表示基类, {}:属性 User ...

  5. php array_unshift()函数 语法

    php array_unshift()函数 语法 作用:用于向数组插入新元素.新数组的值将被插入到数组的开头.富瑞华 语法:array_unshift(array,value1,value2,valu ...

  6. Web防止button按钮点击多次

    BtnPass.Attributes.Add("onclick", "javascript:if(document.getElementById('IsSubmited' ...

  7. 【bzoj2073】【[POI2004]PRZ】位运算枚举子集的特技

    (上不了p站我要死了) Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍在桥上的人都不能超过一 ...

  8. 【JavaMail】JavaMail整合RabbitMq发送邮件案例

    前言 Linux安装RabbitMQ:https://www.cnblogs.com/jxd283465/p/11975094.html SpringBoot整合RabbitMQ:https://ww ...

  9. Improving Network Management with Software Defined Networking

    Name of article:Improving Network Management with  Software Defined Networking Origin of the article ...

  10. Oracle-SQL程序优化案例二

    有时候写得不规范的SQL语句真的是占用很多时间 以下是我在工作中发现的规律,如果字段过多的使用函数,尽量不要将这些字段串联在一起做匹配或查询条件,比如红色注释部分,在执行红色部分的时候 这个SQL程序 ...