python中type()是什么意思-Python教程

资源魔 22 0

type()是一个内建的猎取变量类型的函数。

type()函数有两个用法,当只有一个参数的时分,前往工具的类型。当有三个参数的时分前往一个类工具。

语法:

type(object)
type(name, bases, dict)

详细用法:

一个参数

type(object)

前往一个工具的类型,如:

In [1]: a = 10 
In [2]: type(a)
Out[2]: int

三个参数

tpye(name, bases, dict)

name 类名

bases 父类的元组

dict 类的属性办法以及值组成的键值对

前往一个类工具:

# 实例办法
def instancetest(self):
	print("this is a instance method")
# 类办法
@classmethod
def classtest(cls):
	print("this is a class method")
# 动态办法
@staticmethod
def statictest():
	print("this is a static method")
# 创立类
test_property = {"name": "tom", "instancetest": instancetest, "classtest": classtest, "statictest": statictest}
Test = type("Test", (), test_property)
# 创立工具
test = Test()
# 挪用办法
print(test.name)
test.instancetest()
test.classtest()
test.statictest()

输入后果:

tom
this is a instance method
this is a class method
this is a static method

保举教程:python教程

以上就是python中type()是甚么意义的具体内容,更多请存眷资源魔其它相干文章!

标签: Python python教程 python编程 python使用问题 type()

抱歉,评论功能暂时关闭!