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 相关文章推荐
pyv8学习python和javascript变量进行交互
Dec 04 Python
Python黑魔法@property装饰器的使用技巧解析
Jun 16 Python
python实现二叉树的遍历
Dec 11 Python
详谈套接字中SO_REUSEPORT和SO_REUSEADDR的区别
Apr 28 Python
删除python pandas.DataFrame 的多重index实例
Jun 08 Python
python实现简易内存监控
Jun 21 Python
Python中函数参数匹配模型详解
Jun 09 Python
Django Python 获取请求头信息Content-Range的方法
Aug 06 Python
Python一键查找iOS项目中未使用的图片、音频、视频资源
Aug 12 Python
Python-openCV读RGB通道图实例
Jan 17 Python
如何利用python web框架做文件流下载的实现示例
Jun 02 Python
pycharm 多行批量缩进和反向缩进快捷键介绍
Jan 15 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 如何向 MySQL 发送数据
2006/10/09 PHP
php禁止直接从浏览器输入地址访问.php文件的方法
2014/11/04 PHP
PHP SplObjectStorage使用实例
2015/05/12 PHP
Yii2数据库操作常用方法小结
2017/05/04 PHP
IE DOM实现存在的部分问题及解决方法
2009/07/25 Javascript
仅IE6/7/8中innerHTML返回值忽略英文空格的问题
2011/04/07 Javascript
Prototype源码浅析 String部分(一)之有关indexOf优化
2012/01/15 Javascript
JavaScript设计模式之单例模式实例
2014/09/24 Javascript
轻松创建nodejs服务器(1):一个简单nodejs服务器例子
2014/12/18 NodeJs
js数组去重的5种算法实现
2015/11/04 Javascript
SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
2015/12/10 Javascript
jQuery+ajax实现文章点赞功能的方法
2015/12/31 Javascript
JS简单实现点击复制链接的方法
2016/08/03 Javascript
BootStrap自定义popover,点击区域隐藏功能的实现
2018/01/23 Javascript
node 版本切换的实现
2020/02/02 Javascript
[35:55]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第一场 12.11
2020/12/13 DOTA
python实现360皮肤按钮控件示例
2014/02/21 Python
Python多进程同步Lock、Semaphore、Event实例
2014/11/21 Python
Python连接DB2数据库
2016/08/27 Python
Pytorch入门之mnist分类实例
2018/04/14 Python
python截取两个单词之间的内容方法
2018/12/25 Python
Python基于Tkinter模块实现的弹球小游戏
2018/12/27 Python
python3 自动识别usb连接状态,即对usb重连的判断方法
2019/07/03 Python
tensorflow -gpu安装方法(不用自己装cuda,cdnn)
2020/01/20 Python
Python random模块的使用示例
2020/10/10 Python
Python3+Appium安装及Appium模拟微信登录方法详解
2021/02/16 Python
DNA基因检测和分析:23andMe
2019/05/01 全球购物
优秀员工个人的自我评价
2013/11/29 职场文书
试用期员工考核制度
2014/01/22 职场文书
会计专业自我鉴定
2014/02/10 职场文书
四年大学自我鉴定
2014/02/17 职场文书
土木工程求职信
2014/05/29 职场文书
小学五年级语文上册教学计划
2015/01/22 职场文书
房地产项目合作意向书
2015/05/08 职场文书
36个正则表达式(开发效率提高80%)
2021/11/17 Javascript
pytorch实现加载保存查看checkpoint文件
2022/07/15 Python