Python基于内置函数type创建新类型
作者:lincappu 发布时间:2023-11-18 10:00:29
英文文档:
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')
来源:https://www.cnblogs.com/lincappu/p/8145050.html


猜你喜欢
- 这篇文章主要介绍了Python3的socket使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- mysql删除操作其实是假删除在 InnoDB 中,你的 delete 操作,并不会真的把数据删除,mysql 实际上只是给删除的数据打了个
- 本文实例讲述了Python实现读取机器硬件信息的方法。分享给大家供大家参考,具体如下:本人最近新学python ,用到关于机器的相关信息,经
- 回车和换行的历史:机械打字机有回车和换行两个键作用分别是:换行就是把滚筒卷一格,不改变水平位置。 (即移到下一行,但不是行首,而是和上一行水
- 一、环境搭建1、安装python+pycharm软件 。python安装网址官网:https://www.python.org/about/
- 前言Mysql插入不重复的数据,当大数据量的数据需要插入值时,要判断插入是否重复,然后再插入,那么如何提高效率?解决的办法有很多种,不同的场
- 前言:二分法也就是二分查找,它是一种效率较高的查找方法假如公司新来了一个人,叫张三,他是你们公司第47个人,过了一段时间后,有些人呢看张三不
- 一、前言刚刚学了一些python文件读写的内容,先跑过来整活了。顺便复习一下之前学的东西。import timedoc_local='
- 本文实例讲述了Python统计文件中去重后uuid个数的方法。分享给大家供大家参考。具体如下:利用正则表达式按行获取日志文件中的的uuid,
- 前言由于后端使用php、node.js、java等进行大量的图片下载操作可能会导致服务器负载过高,所以将图片下载转移到客户端是个不错的选择,
- 日前,Mozilla 的 Arun Ranganathan 向 W3C 提交了一个草案,旨在推出一个 JavaScript API,让 Ja
- 前言:随着编程语言的发展,Go 还很年轻。它于 2009 年 11 月 10 日首次发布。其创建者Robert Griesemer Rob
- 字符画,一种由字母、标点、汉字或其他字符组成的图画。简单的字符画是利用字符的形状代替图画的线条来构成简单的人物、事物等形象,它一般由人工制作
- 这样的问题是因为 数据库字符集,表字符集,字段字符集都设为:gbk_chinese_ci 注意数据库连接串里面的 Stmt=Set Name
- numpy.where (condition[, x, y])numpy.where() 有两种用法:1. np.where(conditi
- 无参修饰 ,无参数时不需要调用def log1(func): func()@log1def test(): prin
- js判断某个字符出现的次数的简单实例function patch(re,s){ //参数1正则式,参数2字符串re=eval_r("
- php面试题的题目: $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php
- 本文实例讲述了JS+CSS实现仿雅虎另类滑动门切换效果。分享给大家供大家参考。具体如下:这是仿照雅虎特色服务的一个Tab滑动切换效果,核心是
- 1、为什么淘宝的手机频道页面,竟然会有笔记本、数码相机、随身听,甚至是游戏之类的栏目,而且还有一个“数码·生活”栏目是包括以上这些设备的综合