When I run the following, I got the output as below:
x = [1, 2, 3, 4, 5]
print(type(x))
y = "PYTHON"
print(type(y))
Output:
<class 'list'>
<class 'str'>
I have read that everything in Python is a object, yet I am seeing the output as class. I also read that object is a class. How can an object be a class? I am beginner in python and was unable to understand this. Can you please help me?
-
1what is the desired output?Luis Henrique– Luis Henrique2020年01月02日 18:55:17 +00:00Commented Jan 2, 2020 at 18:55
-
1Does this answer your question? Class vs. Type in PythonLuis Henrique– Luis Henrique2020年01月02日 18:59:08 +00:00Commented Jan 2, 2020 at 18:59
3 Answers 3
An object is an instance of a class. type is identifying the class of which this object is an instance.
Comments
Welcome to stackoverflow.
What you are saying is correct. Both x and y are objects.
The function "type" basically tells you about what is the type of the object.
As x is a list it's of list type. And same goes for y.
Comments
The purpose of the type function is to give you the class of a given object, not the type of the instantiated object.
See the docs: https://docs.python.org/3/library/functions.html#type.