本文摘自php中文网,作者V,侵删。

type()是一个内建的获取变量类型的函数。
type()函数有两个用法,当只有一个参数的时候,返回对象的类型。当有三个参数的时候返回一个类对象。
语法:
1 2 | type(object)
type(name, bases, dict)
|
具体用法:
一个参数
返回一个对象的类型,如:
1 2 3 | In [1]: a = 10
In [2]: type(a)
Out[2]: int
|
三个参数
name 类名
bases 父类的元组
dict 类的属性方法和值组成的键值对
返回一个类对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # 实例方法
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()
|
输出结果:
1 2 3 4 | tom
this is a instance method
this is a class method
this is a static method
|
推荐教程:python教程
以上就是python中type()是什么意思的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python3多线程
Python图像处理的基础和opencv的入门函数
业余学Python有用吗
Python怎么把列表转化为字符串
Python tkinter教程 数字猜谜游戏
Python中socket实现tcp通信的介绍(附示例)
Python二进制怎么转十进制?
怎么把字符串解析成浮点数或者整数
Python安装的第三方库怎么删除
Python怎么求加到n项和
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python中type()是什么意思