python内置函数type(x)的作用_Python内置函数(43)——type

英文文档:

classtype(object)

classtype(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__

#type(a)返回a的类型

>>> type(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

>>> 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内置函数(65)——type

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

Python内置函数(43)——min

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

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

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

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

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

Python内置函数和内置常量

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

python内置函数

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

Python 内置函数笔记

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

python内置函数,匿名函数

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

Python之路(第八篇)Python内置函数、zip()、max()、min()

一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

随机推荐

js实现全选反选功能

开始慢慢地学习js&jQuery. function clicked(){ var arr=document.getElementsByName("product"); f ...

ios auto layout demystified (一)

Ambiguous Layout 在开发过程中,你可以通过调用hasAmbiguousLayout 来测试你的view约束是否足够的.这个会返回boolean值.如果有一个不同的frame就会返回ye ...

【转】C++ 内存分配(new,operator new)详解

本文主要讲述C++ new运算符和operator new, placement new之间的种种关联,new的底层实现,以及operator new的重载和一些在内存池,STL中的应用. 一 new ...

linux 启动 oracle数据库

第一步:切换到oracle用户 su - oracle 第二步:启动oracle数据库监听 lsnrctl start 第三步:输入下方命令,出现:sql> sqlplus /nolog 第四步 ...

Android列表视图ListView和ListActivity-android学习之旅(二十四)

ListView简介 ListView是android中常用的一种控件,创建ListView有两种方式: 1.在xml中使用ListView控件创建. 2.使用activity继承ListActivi ...

python3排序 sorted(key=lambda)

使用python对列表(list)进行排序,说简单也简单,说复杂也复杂,我一开始学的时候也搞不懂在说什么,只能搜索一些英文文章看看讲解,现在积累了一些经验,写在这里跟大家分享,我们通过例子来详细解释一 ...

Mysql简单入门

这两天比较懒,没有学习,这个是我问一个学java的小伙伴要的sql的总结资料,大体语句全在上面了,复制到博客上,以后忘记可以查看 #1命令行连接MySQLmsyql -u root -proot;#2 ...

[转]mii-tool与ethtool的用法详解

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值