Home > Back-end >  Why is a python variable, which is declared(assigned a value),of type class?
Why is a python variable, which is declared(assigned a value),of type class?

Time:11-08

  1. I declare a variable and assign it an integer value, its type is <class 'int>. Shouldn't that be an object?
  2. If I define a class and instantiate its object, the type of the object is again class.
  3. If I assign a variable to object, the variable is <class 'object> and its type is <class 'type'> (refer the corresponding shell entries)

To be precise, I want to understand what it means when (in tutorials) it is said that everything in Python is an object, when I can see the that type always says it's a class? Can anyone please explain this discrepancy?

In Python shell: 1.

>>> a = 1
>>> type(a)
>>> <class 'int'>
>>> class Something:
...    pass
...
>
>>> s = Something()
>>> type(s)
>>> <class '__main__.Something'>
>>> t = object
>>> t
>>> <class 'object'>
>
>>> type(t)
>>> <class 'type'>

CodePudding user response:

All values in Python are objects, in the sense that each value has a type associated with it. (Variables themselves to not have types; type(a) simply reports the inherent type of whatever value as assigned to the name a.)

Further, classes themselves are first-class values with their own type. Just as the type of 3 is int, the type of int is type. Even type is a value, with the somewhat surprising feature that the type of type is type.

object is the root of the class hierarchy, so all classes have object as an ancestor. (Just as type has type type, object is the parent of object, which is probably less surprising if you are used to any class being a trivial subclass of itself.) All classes, including object, are instances of type type.


With the definition (not declaration) a = 1, you assign the value 1 (with type int) to the name a.

With s = Something(), you create a value of type Something and assign it to the name s.

With t = object, you assign the value object (with the type type) to the name t.

CodePudding user response:

Hmm..., this is not only a vocabulary question. In Python 3 the object class is a special class whose any other class (including int) derives. Everything is an object could more verbosely be said everything is an instance of a class that is a subclass of object.

The last one says that most classes including object are direct instances of another very special class which it type. In fact in Python all classes are instances of their metaclass, and except for special requirement the standard metaclass is type and metaclasses are expected to be subclasses of type

If we want to go one step further, we realize that as type being a class, is a subclass of object: type.__mro__ gives (<class 'type'>, <class 'object'>). On a strict logical and mathematical point of view, it can be seen as rather weird (the set of all sets cannot exists in set theory). But as the basic types and functions of Python are built-in they are not required to be defined in Python language and we do not fall in a definition loop.

  • Related