当前位置: 首页 > Python编程 > Python编程实战技能 > Python编程学习教程 > python中type()是什么意思

python中type()是什么意思

发布时间:2020年11月01日 06:45:41 来源:环球青藤 点击量:1369

【摘要】type()是一个内建的获取变量类型的函数。type()函数有两个用法,当只有一个参数的时候,返回对象的类型。当有三个参数的时候返回一个类对象

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教程请关注环球青藤其它相关文章!

分享到: 编辑:wangmin

就业培训申请领取
您的姓名
您的电话
意向课程
点击领取

环球青藤

官方QQ

扫描上方二维码或点击一键加群,免费领取大礼包,加群暗号:青藤。 一键加群

绑定手机号

应《中华人民共和国网络安全法》加强实名认证机制要求,同时为更加全面的体验产品服务,烦请您绑定手机号.

预约成功

本直播为付费学员的直播课节

请您购买课程后再预约

环球青藤移动课堂APP 直播、听课。职达未来!

安卓版

下载

iPhone版

下载
环球青藤官方微信服务平台

刷题看课 APP下载

免费直播 一键购课

代报名等人工服务

课程咨询 学员服务 公众号

扫描关注微信公众号

APP

扫描下载APP

返回顶部