Python的type函数结果你知道嘛_第1页
Python的type函数结果你知道嘛_第2页
Python的type函数结果你知道嘛_第3页
Python的type函数结果你知道嘛_第4页
Python的type函数结果你知道嘛_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第Python的type函数结果你知道嘛目录isinstance()与type()区别:type函数结果举例,主要有六大类:总结简介:type()函数可以对数据的类型进行判定。

isinstance()与type()区别:

type()不会认为子类是一种父类类型,不考虑继承关系。

isinstance()会认为子类是一种父类类型,考虑继承关系。

如果要判断两个类型是否相同推荐使用isinstance()。

type函数结果举例,主要有六大类:

1、标准数据类型。

2、module模块类型:主要来源于模块安装并使用

3、type类型:主要来源于标准数据类型的类对象

4、程序员新增的类,自定义的类型:classmain.XXX、NoneType

5、builtin_function_or_method内置函数或者方法

6、其他拓展类型如:collections.Counter、collections.deque等

源码:

importtime

importrandom

importasyncio

importcollections

#基本数据类型

print(type(1))#class'int'

print(type(3.14))#class'float'

print(type("hello"))#class'str'

print(type([1,2]))#class'list'

print(type((1,"a")))#class'tuple'

print(type({"name":"tom"}))#class'dict'

print(type(False))#class'bool'

print("*"*30)

#class'module'

print(type(time))

print(type(random))

print(type(asyncio))

print("*"*30)

#class'type'

print(type(type))

print(type(int))

print(type(float))

print(type(bool))

print(type(str))

print(type(dict))

print(type(list))

print(type(tuple))

print(type(set))

print("*"*30)

#自定义的类型:class'__main__.XXX'

classA:

x=111

def__init__(self):

self.x=1

defrun(self):

pass

@staticmethod

defsay():

pass

@classmethod

deflive(cls):

pass

@property

defsleep(self):

pass

a=A()

print(type(A))#class'type'

print(type(object))#class'type'

print(type(a))#class'__main__.A'

#class'NoneType'

print(type(a.__init__()))

print(type(a.run()))

print(type(a.say()))

print(type(a.live()))

print(type(a.sleep))

print(type(A.x))#class'int'与初始值类型一致

print(type(a.x))#class'int'与初始值类型一致

print("*"*30)

#class'builtin_function_or_method'

print(type(None))

print(type(bin))

print(type(len))

print(type(min))

print(type(dir))

print("*"*30)

data="message"

result=collections.Counter(data)

dict1=collections.OrderedDict({"name":"Tom","age":25,"address":"CN"})

deq1=collections.deque("abc")

print(type(result))#class'collections.Counter'

print(type(dict1))#class'collections.OrderedDict'

print(type(deq1))#class'collections.deque'

实际应用举例:

1、判定是否是lambda类型

2、判定是否是函数类型

3、判定是否是方法

4、判定生成器类型等

源码:

fromtypesimportLambdaType,MethodType,GeneratorType,FunctionType,BuiltinFunctionType

test1=lambdax:x+1

#判定是否是lambda类型。需要注意的是lambda就是函数类型,本质是一样的

print(type(test1)==LambdaType)#True

#判定是否是函数类型

print(type(test1)==FunctionType)#True

#判定是否是内置函数类型

print(type(bin)==BuiltinFunctionType)#True

classTest2:

defrun(self):

pass

test2=Test2()

#判定是否是方法

print(type(test2.run)==

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论