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学习之面向对象【入门初级篇】
Jan 21 Python
轻量级的Web框架Flask 中模块化应用的实现
Sep 11 Python
Python模拟脉冲星伪信号频率实例代码
Jan 03 Python
Tornado高并发处理方法实例代码
Jan 15 Python
Python实现批量压缩图片
Jan 25 Python
Python2和Python3.6环境解决共存问题
Nov 09 Python
python3使用matplotlib绘制散点图
Mar 19 Python
浅谈django url请求与数据库连接池的共享问题
Aug 29 Python
Python3常见函数range()用法详解
Dec 30 Python
基于python3实现倒叙字符串
Feb 18 Python
python怎么判断素数
Jul 01 Python
Django正则URL匹配实现流程解析
Nov 13 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
桌面中心(二)数据库写入
2006/10/09 PHP
解决File size limit exceeded 错误的方法
2013/06/14 PHP
CI框架(ajax分页,全选,反选,不选,批量删除)完整代码详解
2016/11/01 PHP
jquery弹窗插件colorbox绑定动态生成元素的方法
2014/06/20 Javascript
判断日期是否能跨月查询的js代码
2014/07/25 Javascript
jQuery使用fadeout实现元素渐隐效果的方法
2015/03/27 Javascript
javascript表格的渲染组件
2015/07/03 Javascript
jQuery增加与删除table列的方法
2016/03/01 Javascript
深入理解React中es6创建组件this的方法
2016/08/29 Javascript
Jquery删除css属性的简单方法
2016/12/04 Javascript
JS实现点击表头表格自动排序(含数字、字符串、日期)
2017/01/22 Javascript
详解RequireJS按需加载样式文件
2017/04/12 Javascript
node.js实现微信JS-API封装接口的示例代码
2017/09/06 Javascript
浅谈Vue网络请求之interceptors实际应用
2018/02/28 Javascript
vue升级之路之vue-router的使用教程
2018/08/14 Javascript
nodejs微信开发之自动回复的实现
2019/03/17 NodeJs
解决layui中onchange失效以及form动态渲染失效的问题
2019/09/27 Javascript
python 判断自定义对象类型
2009/03/21 Python
python原始套接字编程示例分享
2014/02/21 Python
用C++封装MySQL的API的教程
2015/05/06 Python
Python中super的用法实例
2015/05/28 Python
Python矩阵常见运算操作实例总结
2017/09/29 Python
python爬虫_微信公众号推送信息爬取的实例
2017/10/23 Python
Python if语句知识点用法总结
2018/06/10 Python
Python3.4 splinter(模拟填写表单)使用方法
2018/10/13 Python
pygame游戏之旅 python和pygame安装教程
2018/11/20 Python
python如何将多个PDF进行合并
2019/08/13 Python
python高级特性简介
2020/08/13 Python
基于Pytorch版yolov5的滑块验证码破解思路详解
2021/02/25 Python
全球最大最受欢迎的旅游社区:Tripadvisor
2017/11/03 全球购物
外企财务年会演讲稿
2014/01/03 职场文书
护理中职生求职信范文
2014/02/24 职场文书
励志演讲稿3分钟
2014/08/21 职场文书
php远程请求CURL案例(爬虫、保存登录状态)
2021/04/01 PHP
Python使用socket去实现TCP客户端和TCP服务端
2022/04/12 Python
选购到合适的激光打印机
2022/04/21 数码科技