概述
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中type()是什么意思的全部内容,希望文章能够帮你解决python中type()是什么意思所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复