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. IAR

    IAR是什么 支持众多半导体公司产品的c处理器 http://www.rimelink.com/pr.jsp

  2. QKeyEvent

    #include<QKeyEvent>键盘事件由两个重载函数,实现即可:  void keyReleaseEvent(QKeyEvent *event);  void keyPressEv ...

  3. c调用c++

    g++ main.c math.cpp math.h中加入extern "C"

  4. python 删除/app/*/logs/*/*.logs指定多少天的文件

    # encoding: utf-8 import sys import getopt import os import glob import time import datetime def rem ...

  5. 什么是UAT

    基本概念 UAT,英文User Acceptance Test的简写,也就是用户验收测试,或用户可接受测试,系统开发生命周期方法论的一个阶段,这时相关的用户或独立测试人员根据测试计划和结果对系统进行测 ...

  6. 初学oracle第三天

    体系结构 Oracle 采取的是 Client/Server 架构. 3.1 Client   3.1.1 Sqlplus 这是一个轻量级的功能强大的客户端, 是 dba 必须掌握的工具. 我们可以配 ...

  7. hadoop中yarn

    一.yarn的概述 Apache Yarn(Yet Another Resource Negotiator的缩写)是hadoop集群资源管理器系统,Yarn从hadoop 2引入,最初是为了改善Map ...

  8. Python内置函数之filter map reduce

    Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...

  9. hdu 6214 : Smallest Minimum Cut 【网络流】

    题目链接 ISAP写法 #include <bits/stdc++.h> using namespace std; typedef long long LL; namespace Fast ...

  10. visual studio 2017激活

    VS2017专业版和企业版激活密钥   Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB- ...