英文文档:

class type(object)

class type(namebasesdict)

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内置函数(43)——type的更多相关文章

  1. Python内置函数(65)——type

    英文文档: class type(object) class type(name, bases, dict) With one argument, return the type of an obje ...

  2. Python内置函数(43)——min

    英文文档: min(iterable, *[, key, default]) min(arg1, arg2, *args[, key]) Return the smallest item in an ...

  3. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  4. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  5. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  6. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  7. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  8. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  9. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

随机推荐

  1. epoll 实现回射服务器

    epoll是I/O复用模型中相对epoll和select更高效的实现对套接字管理的函数. epoll有两种模式 LT 和 ET 二者的差异在于 level-trigger 模式下只要某个 socket ...

  2. Linux系统命令归纳

    常规操作命令: # netstat -atunpl |egrep "mysql|nginx"# vimdiff php.ini*# runlevel# rpm -e httpd - ...

  3. python学习03-数据类型

    一.基本数据类型--数字 布尔型 bool型只有两个值:True和False 之所以将bool值归类为数字,是因为我们也习惯用1表示True,0表示False. 以下是布尔值是False的各种情况: ...

  4. Python 中的登陆获取数据跳转页面(不含数据库)

    简单表单和模板: import os.path import tornado.httpserver import tornado.ioloop import tornado.options impor ...

  5. Gradient Descent

    理自Andrew Ng的machine learning课程. 目录: 梯度下降算法 梯度下降算法的直观展示 线性回归中的梯度下降 前提: 线性回归模型 :$h(\theta_0,\theta_1)= ...

  6. 常用的Maven依赖

    一.数据库类型 1.mysql驱动 <!-- mysql驱动支持 --> <dependency> <groupId>mysql</groupId> & ...

  7. Java NIO系列教程(六) 多路复用器Selector

    多路复用器Selector是Java NIO编程的基础,熟练地掌握Selector对于掌握NIO编程至关重要.多路复用器提供选择已经就绪的任务的能力.简单来讲,Selector会不断地轮询注册在其上的 ...

  8. 使用Java编译思想

    1.Java常见的注释有哪些,语法是怎样的? 1)单行注释用//表示,编译器看到//会忽略该行//后的所文本 2)多行注释/* */表示,编译器看到/*时会搜索接下来的*/,忽略掉/* */之间的文本 ...

  9. Online database documentation.

    贫道2018年1月正式出道,可以说在IT界我就是个菜鸟.但我有着一颗不服输的心,我相信我会在这条路走上巅峰之道的.下面我来写我的第一份学习笔记: 介绍:大多数公司都有自己的数据文档,估计大多数都是用P ...

  10. OSM数据下载地址

    1.OSM数据下载地址 官网下载: http://planet.openstreetmap.org/ GeoFabrik:http://www.geofabrik.de/ Metro Extracts ...