[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 ...
随机推荐
- Git Fast Forward 和 no fast foward
如果执行了 Fast Forward,开发者根本不会看到这个分支,就像在 master 直接 commit 一样.
- 构建游戏开发的大数据项目的流程demo图
- 【串线篇】spring boot嵌入式Servlet容器启动原理;
什么时候创建嵌入式的Servlet容器工厂?什么时候获取嵌入式的Servlet容器并启动Tomcat: 获取嵌入式的Servlet容器工厂: 1).SpringBoot应用启动运行run方法 2).r ...
- CF261E Maxim and Calculator (质数,完全背包)
CF261E Maxim and Calculator 题目大意: 有两个初始参数 $ a=1 $ , $ b=0 $ ,你可以对它们进行两个操作: $ b~+=1 $ 或 $ a~\times =b ...
- VS插件CodeRush for Visual Studio发布v18.2.9|附下载
CodeRush能帮助你以极高的效率创建和维护源代码.Consume-first 申明,强大的模板,智能的选择工具,智能代码分析和创新的导航以及一个无与伦比的重构集,在它们的帮助下能够大大的提高你效率 ...
- 用C语音编写python的扩展模块,也就是python调c库
用C语音编写python的扩展模块,也就是python调c库 1.用C语言扩展Python的功能: http://www.ibm.com/developerworks/cn/linux/l-pyt ...
- JavaScript三元运算符以及运算符顺序
三目运算符(三元运算符) 三目运算符:运算符需要三个操作 语法:表达式1?表达式2:表达式3 表达式1是一个条件,值为Boolean类型 若表达式1的值为true,则执行表达式2的操作,并且以表达式2 ...
- makefile filter &&filter-out
sources := foo.c bar.c baz.s abc.h foo: $(sources) gcc $(filter %.c %.s,$(sources)) -o ...
- 运行PHP出现No input file specified错误解决办法
配置了一台新服务器,使用的是IIS + Fastcgi + PHP 5.3.X,访问php页面的时候就会报错“No input file specified” 在php.ini文件里面修改: 1.增加 ...
- BM板子
BM线性递推 玄学玩意 struct LinearRecurrence { using int64 = long long; using vec = std::vector<int64>; ...