What does type() mean in python

王林
Release: 2020-05-11 11:51:16
Original
32821 people have browsed it

What does type() mean in python

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)
Copy after login

Specific usage:

A parameter

type(object)
Copy after login

Returns the type of an object, such as:

In [1]: a = 10 
In [2]: type(a)
Out[2]: int
Copy after login

Three parameters

tpye(name, bases, dict)
Copy after login

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()
Copy after login

Output result:

tom
this is a instance method
this is a class method
this is a static method
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template