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 18 Python
python版简单工厂模式
Oct 16 Python
Python3.5.3下配置opencv3.2.0的操作方法
Apr 02 Python
对pandas的算术运算和数据对齐实例详解
Dec 22 Python
Python数据类型之String字符串实例详解
May 08 Python
搭建python django虚拟环境完整步骤详解
Jul 08 Python
pytorch自定义初始化权重的方法
Aug 17 Python
使用virtualenv创建Python环境及PyQT5环境配置的方法
Sep 10 Python
Python3打包exe代码2种方法实例解析
Feb 17 Python
如何验证python安装成功
Jul 06 Python
Django限制API访问频率常用方法解析
Oct 12 Python
Python实现简单的2048小游戏
Mar 01 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 创建文件(文件夹)以及目录操作代码
2010/03/04 PHP
php中检查文件或目录是否存在的代码小结
2012/10/22 PHP
Zend Framework开发入门经典教程
2016/03/23 PHP
PHP通过调用新浪API生成t.cn格式短网址链接的方法详解
2019/02/20 PHP
PHP微信发送推送消息乱码的解决方法
2019/02/28 PHP
Mozilla中显示textarea中选择的文字
2006/09/07 Javascript
js中最容易被忽视的事件问题大总结
2016/05/15 Javascript
基于angularJS的表单验证指令介绍
2016/10/21 Javascript
js 实现一些跨浏览器的事件方法详解及实例
2016/10/27 Javascript
js print打印网页指定区域内容的简单实例
2016/11/01 Javascript
分享bootstrap学习笔记心得(组件及其属性)
2017/01/11 Javascript
详解vue2.0 使用动态组件实现 Tab 标签页切换效果(vue-cli)
2017/08/30 Javascript
微信小程序 如何引入外部字体库iconfont的图标
2018/01/31 Javascript
axios的拦截请求与响应方法
2018/08/11 Javascript
Vue使用.sync 实现父子组件的双向绑定数据问题
2019/04/04 Javascript
微信公众平台 客服接口发消息的实现代码(Java接口开发)
2019/04/17 Javascript
小程序关于请求同步的总结
2019/05/05 Javascript
ES6中Promise的使用方法实例总结
2020/02/18 Javascript
JS组件库AlloyTouch实现图片轮播过程解析
2020/05/29 Javascript
微信小程序实现列表滚动头部吸顶的示例代码
2020/07/12 Javascript
[01:13:59]LGD vs Mineski Supermajor 胜者组 BO3 第三场 6.5
2018/06/06 DOTA
[00:20]TI9观赛名额抽取Ⅱ
2019/07/24 DOTA
在Python中使用M2Crypto模块实现AES加密的教程
2015/04/08 Python
Python中方法链的使用方法
2016/02/23 Python
利用aardio给python编写图形界面
2017/08/21 Python
Python字典数据对象拆分的简单实现方法
2017/12/05 Python
WIn10+Anaconda环境下安装PyTorch(避坑指南)
2019/01/30 Python
Python提取特定时间段内数据的方法实例
2019/04/01 Python
JetBrains PyCharm(Community版本)的下载、安装和初步使用图文教程详解
2020/03/19 Python
django xadmin中form_layout添加字段显示方式
2020/03/30 Python
Python实现播放和录制声音的功能
2020/08/12 Python
实例讲解CSS3中的box-flex弹性盒属性布局
2016/06/09 HTML / CSS
商铺消防安全责任书
2014/07/29 职场文书
三潭印月的导游词
2015/02/12 职场文书
2016年小学生寒假总结
2015/10/10 职场文书
详细聊聊关于Mysql联合查询的那些事儿
2021/10/24 MySQL