Python基于内置函数type创建新类型


Posted in Python onOctober 22, 2020

英文文档:

class type(object)

class type(name, bases, dict)

With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__.

The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.

With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The namestring is the class name and becomes the __name__ attribute; the bases tuple itemizes the base classes and becomes the __bases__ attribute; and the dict dictionary is the namespace containing definitions for class body and is copied to a standard dictionary to become the __dict__ attribute.

返回对象的类型,或者根据传入的参数创建一个新的类型

说明:

1. 函数只传入一个参数时,返回参数对象的类型。 返回值是一个类型对象,通常与对象.__ class__返回的对象相同。

#定义类型A
>>> class A:
  name = 'defined in A'

#创建类型A实例a
>>> a = A()

#a.__class__属性
>>> a.__class__
<class '__main__.A'>

#type(a)返回a的类型
>>> type(a)
<class '__main__.A'>

#测试类型
>>> type(a) == A
True

 2. 虽然可以通过type函数来检测一个对象是否是某个类型的实例,但是更推荐使用isinstance函数,因为isinstance函数考虑了父类子类间继承关系。

#定义类型B,继承A
>>> class B(A):
  age = 2

#创建类型B的实例b
>>> b = B()

#使用type函数测试b是否是类型A,返回False
>>> type(b) == A
False

#使用isinstance函数测试b是否类型A,返回True
>>> isinstance(b,A)
True

 3. 函数另一种使用方式是传入3个参数,函数将使用3个参数来创建一个新的类型。其中第一个参数name将用作新的类型的类名称,即类型的__name__属性;第二个参数是一个元组类型,其元素的类型均为类类型,将用作新创建类型的基类,即类型的__bases__属性;第三个参数dict是一个字典,包含了新创建类的主体定义,即其值将复制到类型的__dict__属性中。

#定义类型A,含有属性InfoA
>>> class A(object):
  InfoA = 'some thing defined in A'

#定义类型B,含有属性InfoB
>>> class B(object):
  InfoB = 'some thing defined in B'

#定义类型C,含有属性InfoC
>>> class C(A,B):
  InfoC = 'some thing defined in C'

#使用type函数创建类型D,含有属性InfoD
>>> D = type('D',(A,B),dict(InfoD='some thing defined in D'))

#C、D的类型
>>> C
<class '__main__.C'>
>>> D
<class '__main__.D'>

#分别创建类型C、类型D的实例
>>> c = C()
>>> d = D()

#分别输出实例c、实例b的属性
>>> (c.InfoA,c.InfoB,c.InfoC)
('some thing defined in A', 'some thing defined in B', 'some thing defined in C')
>>> (d.InfoA,d.InfoB,d.InfoD)
('some thing defined in A', 'some thing defined in B', 'some thing defined in D')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python多线程http下载实现示例
Dec 30 Python
python中元类用法实例
Oct 10 Python
使用优化器来提升Python程序的执行效率的教程
Apr 02 Python
Python中的并发处理之asyncio包使用的详解
Apr 03 Python
python获取交互式ssh shell的方法
Feb 14 Python
使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)
May 28 Python
python如何解析配置文件并应用到项目中
Jun 27 Python
python Gunicorn服务器使用方法详解
Jul 22 Python
Python scipy的二维图像卷积运算与图像模糊处理操作示例
Sep 06 Python
python实现大学人员管理系统
Oct 25 Python
tensorflow多维张量计算实例
Feb 11 Python
python 如何上传包到pypi
Dec 24 Python
python使用ctypes库调用DLL动态链接库
Oct 22 #Python
Python通过len函数返回对象长度
Oct 22 #Python
python 还原梯度下降算法实现一维线性回归
Oct 22 #Python
利用Pycharm + Django搭建一个简单Python Web项目的步骤
Oct 22 #Python
python处理写入数据代码讲解
Oct 22 #Python
基于Python爬取股票数据过程详解
Oct 21 #Python
OpenCV利用python来实现图像的直方图均衡化
Oct 21 #Python
You might like
针对初学PHP者的疑难问答(1)
2006/10/09 PHP
PHP与服务器文件系统的简单交互
2016/10/21 PHP
php正则去除网页中所有的html,js,css,注释的实现方法
2016/11/03 PHP
jQuery的实现原理的模拟代码 -5 Ajax
2010/08/07 Javascript
onkeypress字符按键兼容所有浏览器使用介绍
2013/04/24 Javascript
javascript对象的使用和属性操作示例详解
2014/03/02 Javascript
jQuery实现下拉框左右移动(全部移动,已选移动)
2016/04/15 Javascript
自带气泡提示的vue校验插件(vue-verify-pop)
2017/04/07 Javascript
详解React之父子组件传递和其它一些要点
2018/06/25 Javascript
Vue.js的复用组件开发流程完整记录
2018/11/29 Javascript
vue组件内部引入外部js文件的方法
2020/01/18 Javascript
[10:39]DOTA2上海特级锦标赛音乐会纪录片
2016/03/21 DOTA
[00:32]2018DOTA2亚洲邀请赛EG出场
2018/04/03 DOTA
Python计算已经过去多少个周末的方法
2015/07/25 Python
Python元组操作实例分析【创建、赋值、更新、删除等】
2017/07/24 Python
用PyInstaller把Python代码打包成单个独立的exe可执行文件
2018/05/26 Python
在Python中输入一个以空格为间隔的数组方法
2018/11/13 Python
简单了解python中的与或非运算
2019/09/18 Python
Python爬取365好书中小说代码实例
2020/02/28 Python
15个Pythonic的代码示例(值得收藏)
2020/10/29 Python
Larsson & Jennings官网:现代瑞士钟表匠
2018/03/20 全球购物
美国儿童玩具、装扮和玩偶商店:Magic Cabin
2018/09/02 全球购物
如何启动时不需输入用户名与密码
2014/05/09 面试题
JDK安装目录下有哪些内容
2014/08/25 面试题
单位委托书范本
2014/04/04 职场文书
小学生感恩演讲稿
2014/04/25 职场文书
优秀安全员事迹材料
2014/05/11 职场文书
自强之星事迹材料
2014/05/12 职场文书
热门专业求职信
2014/05/24 职场文书
政协调研汇报材料
2014/08/15 职场文书
大学生自荐信范文
2015/03/05 职场文书
关于应聘教师的自荐信
2016/01/28 职场文书
如何让2019年上半年的工作总结更出色!
2019/07/01 职场文书
某某店铺的开业庆典主持词范本
2019/11/25 职场文书
SQL Server作业失败:无法确定所有者是否有服务器访问权限的解决方法
2021/06/30 SQL Server
http通过StreamingHttpResponse完成连续的数据传输长链接方式
2022/02/12 Python