python中的type函数的用法-创新互联
这篇文章主要为大家详细介绍了python中的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中的type函数的用法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果喜欢这篇文章,不如把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页标题:python中的type函数的用法-创新互联
网站地址:http://www.scyingshan.cn/article/cdgjgp.html


咨询
建站咨询
