[Python3 填坑] 018 组装类的几个例子
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 组装类的几个例子的更多相关文章
- [Python3 填坑] 006 “杠零”,空字符的使用
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...
- [Python3 填坑] 014 类的常用魔术方法举例
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 __init__() 2.2 __new__() 2.3 __call__() 2.4 __str__() 2.5 __repr__() ...
- [Python3 填坑] 013 几个类相关函数的举例
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 issubclass() 2.2 isinstance() 2.3 hasattr() 2.4 getattr() 2.5 setatt ...
- [Python3 填坑] 009 深拷贝与浅拷贝
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...
- [Python3 填坑] 004 关于八进制
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...
- [Python3 填坑] 017 实例方法、静态方法、类方法的区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 先上例子 2.2 分析 1. print( 坑的信息 ) 挖坑时间:2019/04/07 明细 坑的编码 内容 Py024-1 实例方法 ...
- [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...
- [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...
- [Python3 填坑] 005 如何“响铃”
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 1. print( 坑的信息 ) 挖坑时间:2019/01/08 明细 坑的编码 内容 Py004-2 ...
随机推荐
- qthread线程
一般调用quit()函数之后可以紧接着调用wait()函数确保线程退出.sleep()等让线程休眠的函数不需要调用,因为Qt中线程是事件驱动机制.但是如果是继承的QTHread类,在run()函数中使 ...
- VMware虚拟机磁盘文件vmdk单文件转多文件相互转换
设置环境变量 set PATH=%PATH%;D:\Program Files (x86)\VMware\VMware Workstation echo %PATH% C:\Users\Admi ...
- 继承父类的注入Bean
Bcontroller 继承了 Acontroller ,Acontroller注入了一个API,通过API实现了一个功能“方法X”.在Bcontroller中调用 Acontroller 的“方法X ...
- sql视频学习关键笔记(自用记单词与学习用)
sql字段类型 numeric(18,3)-18位整数加3位小数点(货币计量最好选这类型) sql关键字 insert. update. delete alter grant 授权. revoke 回 ...
- 设计模式Design Pattern(3) -- 责任链模式
什么是责任链模式? 责任链模式(Chain of Responsibility Pattern):请求知道公开接口,但不知道那个具体类处理,这些具体处理类对象连接成一条链.请求沿着这条链传递,直到有对 ...
- 【leetcode】1154. Day of the Year
题目如下: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the ...
- Spring AOP expose-proxy
写在前面 expose-proxy.为是否暴露当前代理对象为ThreadLocal模式. SpringAOP对于最外层的函数只拦截public方法,不拦截protected和private方法(后续讲 ...
- postman—Sandbox和断言
Postman沙盒 Postman Sandbox是一个JavaScript执行环境,您可以在编写预请求脚本和测试脚本(在Postman和Newman中)时可用.在这个沙箱中执行您在预请求/测试脚本部 ...
- linux运维、架构之路-Git+Jenkins实现自动化部署
一.Jenkins介绍 jenkins是一个用JAVA编写的开源的持续集成工具,运行在servlet容器中,支持软件配置管理(SCM)工具,可以执行基于APACHE ANT和APAC ...
- 进阶:python3实现 插入排序
一图胜千言,插入排序的核心逻辑如下: 将数据分成两半,前一半是已排好序的,后一半是待排序的 每次取后一半中的第一个数,在已排序的一半中,逆序依次比较,找到要插入的位置 记录插入的位置,在最后判断是否需 ...