type() is a built-in function to get the type of a variable.
The type() function has two uses. When there is only one parameter, it returns the type of the object. Returns a class object when there are three parameters.
Syntax:
type(object) type(name, bases, dict)
Specific usage:
A parameter
type(object)
Returns the type of an object, such as:
In [1]: a = 10 In [2]: type(a) Out[2]: int
Three parameters
tpye(name, bases, dict)
name class name
bases tuple of parent class
dict key-value pair composed of attribute methods and values of the class
Return a class object:
# 实例方法 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()
Output result:
tom this is a instance method this is a class method this is a static method
Recommended tutorial: python tutorial
The above is the detailed content of What does type() mean in python. For more information, please follow other related articles on the PHP Chinese website!