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实现的可以拷贝或剪切一个文件列表中的所有文件
Apr 30 Python
Python打印scrapy蜘蛛抓取树结构的方法
Apr 08 Python
Python中使用装饰器时需要注意的一些问题
May 11 Python
在Django框架中运行Python应用全攻略
Jul 17 Python
python flask实现分页效果
Jun 27 Python
python中多个装饰器的执行顺序详解
Oct 08 Python
Python 计算任意两向量之间的夹角方法
Jul 05 Python
django 前端页面如何实现显示前N条数据
Mar 16 Python
Python自带的IDE在哪里
Jul 01 Python
python tqdm实现进度条的示例代码
Nov 10 Python
Python3 如何开启自带http服务
May 18 Python
浅析Python实现DFA算法
Jun 26 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
东方红 - 来复式再生机的修复
2021/03/02 无线电
php 算法之实现相对路径的实例
2017/10/17 PHP
如何优雅的使用 laravel 的 validator验证方法
2018/11/11 PHP
javascript Object与Function使用
2010/01/11 Javascript
javascript中检测变量的类型的代码
2010/12/28 Javascript
JS从一组数据中找到指定的单条数据的方法
2016/06/02 Javascript
JavaScript中windows.open()、windows.close()方法详解
2016/07/28 Javascript
浅谈angular2路由预加载策略
2017/10/04 Javascript
jquery使用iscorll实现上拉、下拉加载刷新
2017/10/26 jQuery
解决Vue2.x父组件与子组件之间的双向绑定问题
2018/03/06 Javascript
JS实现的字符串数组去重功能小结
2019/06/17 Javascript
详解微信小程序自定义组件的实现及数据交互
2019/07/22 Javascript
vue路由传参页面刷新参数丢失问题解决方案
2019/10/08 Javascript
详解JavaScript之ES5的继承
2020/07/08 Javascript
深入了解Vue动态组件和异步组件
2021/01/26 Vue.js
python分布式环境下的限流器的示例
2017/10/26 Python
python3+PyQt5实现支持多线程的页面索引器应用程序
2018/04/20 Python
Tensorflow分类器项目自定义数据读入的实现
2019/02/05 Python
Python日期时间Time模块实例详解
2019/04/15 Python
django 中的聚合函数,分组函数,F 查询,Q查询
2019/07/25 Python
django之静态文件 django 2.0 在网页中显示图片的例子
2019/07/28 Python
django框架创建应用操作示例
2019/09/26 Python
Python文件操作函数用法实例详解
2019/12/24 Python
vue学习笔记之动态组件和v-once指令简单示例
2020/02/29 Python
PyQt5 界面显示无响应的实现
2020/03/26 Python
Python SMTP配置参数并发送邮件
2020/06/16 Python
python logging模块的使用
2020/09/07 Python
美国一家著名的手表在线折扣网站:Discount Watch Store
2020/02/24 全球购物
会计职业生涯规划范文
2014/01/04 职场文书
《跟踪台风的卫星》教学反思
2014/04/10 职场文书
2014年教师教学工作总结
2014/11/08 职场文书
幼师求职自荐信
2015/03/26 职场文书
关于开学的感想
2015/08/10 职场文书
Oracle创建只读账号的详细步骤
2021/06/07 Oracle
win11怎么用快捷键锁屏? windows11锁屏的几种方法
2021/11/21 数码科技
tomcat正常启动但网页却无法访问的几种解决方法
2022/05/06 Servers