-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?

METALHEAD
  • 2,734
  • 3
  • 22
  • 37

3 Answers3

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.

Avi Kaminetzky
  • 1,489
  • 2
  • 19
  • 42
  • 1
    What is the difference? – Scott Hunter Jan 02 '20 at 19:01
  • 1
    That doesn't explain the difference between "the class of a given object" and "type of the instantiated object" – Sayse Jan 02 '20 at 19:04
  • @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 – Sayse Jan 02 '20 at 19:07
0

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

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
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.

Swetank Poddar
  • 1,257
  • 9
  • 23