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的Django框架部署的一些建议
Apr 09 Python
Python对列表中的各项进行关联详解
Aug 15 Python
Python模块WSGI使用详解
Feb 02 Python
pandas创建新Dataframe并添加多行的实例
Apr 08 Python
python实现超市扫码仪计费
May 30 Python
Python实现数据可视化看如何监控你的爬虫状态【推荐】
Aug 10 Python
Pycharm 设置默认头的图文教程
Jan 17 Python
Python函数参数匹配模型通用规则keyword-only参数详解
Jun 10 Python
python-django中的APPEND_SLASH实现方法
Jun 21 Python
Python使用Opencv实现图像特征检测与匹配的方法
Oct 30 Python
Python tkinter实现图片标注功能(完整代码)
Dec 08 Python
Python中用xlwt制作表格实例讲解
Nov 05 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分页函数
2006/07/08 PHP
php 目录遍历、删除 函数的使用介绍
2013/04/28 PHP
php实现根据url自动生成缩略图的方法
2014/09/23 PHP
Symfony模板的快捷变量用法实例
2016/03/17 PHP
Yii2 rbac权限控制之rule教程详解
2016/06/23 PHP
PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)实例详解
2018/04/20 PHP
JQuery跨Iframe选择实现代码
2010/08/19 Javascript
JavaScript 设计模式 安全沙箱模式
2010/09/24 Javascript
基于jquery的禁用右键、文本选择功能、复制按键的实现代码
2013/08/27 Javascript
JS对话框_JS模态对话框showModalDialog用法总结
2014/01/11 Javascript
在JS中操作时间之getUTCMilliseconds()方法的使用
2015/06/10 Javascript
HTML5 Shiv完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
2015/11/25 Javascript
js右下角弹出提示框示例代码
2016/01/12 Javascript
jQuery代码实现图片墙自动+手动淡入淡出切换效果
2016/05/09 Javascript
Form表单按回车自动提交表单的实现方法
2016/11/18 Javascript
bootstrap table实例详解
2017/01/06 Javascript
JavaScript制作简单的框选图表
2017/05/15 Javascript
彻底搞懂JavaScript中的apply和call方法(必看)
2017/09/18 Javascript
JavaScript 中的 this 简单规则
2017/09/19 Javascript
jQuery实现基本淡入淡出效果的方法详解
2018/09/05 jQuery
实例讲解JavaScript截取字符串
2018/11/30 Javascript
在Vue项目中用fullcalendar制作日程表的示例代码
2019/08/04 Javascript
vue使用原生swiper代码实例
2020/02/05 Javascript
Python re 模块findall() 函数返回值展现方式解析
2019/08/09 Python
python3.9和pycharm的安装教程并创建简单项目的步骤
2021/02/03 Python
HTML5+lufylegend实现游戏中的卷轴
2016/02/29 HTML / CSS
美国首屈一指的高品质珠宝设计师和零售商:Allurez
2018/01/23 全球购物
土耳其风格手工珠宝:Ottoman Hands
2019/07/26 全球购物
青年教师典范事迹材料
2014/01/31 职场文书
大型营销活动计划书
2014/04/28 职场文书
九一八事变纪念日演讲稿
2014/09/14 职场文书
场地使用证明模板
2014/10/25 职场文书
2014年个人年终总结
2015/03/09 职场文书
SpringBoot 集成Redis 过程
2021/06/02 Redis
Go Plugins插件的实现方式
2021/08/07 Golang
Java死锁的排查
2022/05/11 Java/Android