-4

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?

2
  • 1
    what is the desired output? Commented Jan 2, 2020 at 18:55
  • 1
    Does this answer your question? Class vs. Type in Python Commented Jan 2, 2020 at 18:59

3 Answers 3

0

An object is an instance of a class. type is identifying the class of which this object is an instance.

Sign up to request clarification or add additional context in comments.

Comments

0

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

0

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.

3 Comments

What is the difference?
That doesn't explain the difference between "the class of a given object" and "type of the instantiated object"
@AviKaminetzky - I understand your analogy in terms of difference between class and an instance, but that doesn't appear to be what your initial statement is comparing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.