• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python中type()是什么意思

python 搞代码 4年前 (2022-01-09) 37次浏览 已收录 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")# 类方法@classmethoddef classtest(cls):	print("this is a class method")# 静态方法@staticmethoddef <i>本文来源gaodai$ma#com搞$$代**码网</i>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()

输出结果:

tomthis is a instance methodthis is a class methodthis is a static method

推荐教程:python教程

以上就是python中type()是什么意思的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python中type()是什么意思

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址