How everything in Python is an Object?

How everything in Python is an Object?
How everything in Python is an Object?

Python deservedly has a reputation for being an easy language to read and write. It’s got great documentation and a community that is very welcoming to beginners.

As we dig deeper, we may find many aspects of the Python language that surprise us, as one aspect that deserves in-depth explanation is about How Everything in Python is an Object? Since Python is an object-oriented programming language, and hence everything in Python is an object, every integer, string, list and functions. Before discussing how everything in Python is object lets discuss What is an object?

What is an object ?

Since we already know it that Python is an object-oriented programming language and also that Everything in Python is an object, so the main point of discussion here is firstly what is an object? So In object-oriented programming languages like Python, an object is an entity that contains data along with associated metadata or functionality, these data contained in an object are known as the object’s data attributes. These attributes are simply variables that reference the data. The procedures or series of activities conducted in a certain order or manner that an object performs are known as the methods. An object’s methods are functions that perform operations on the objects data attributes.

There can be multiple instances of an object in a program. Different programming languages define “object” in different ways. In some, it means that all objects must-have attributes and methods while in others, it simply means that all objects are subclassable. In Python programming language its definition is looser as some objects have neither attributes nor methods and also not all objects are subclassable.

But the statement that “Everything in Python is an object” means in the sense that it can be assigned to a variable or passed as an argument to a function. So the objects are the building blocks of an object-oriented program, as the program that uses object-oriented technology is basically a collection of objects. Hence programmes written in python is also a collection of these objects in the form of variables. So it is important to revise that everything in Python objects it means that the strings are objects, Lists are objects, Functions are objects and Even modules are also objects.

Everything in Python is Object:

In Python, pretty much everything is an object, whether it is a number, a function or even modules. Python is using a pure object model where classes are instances of a meta-class “type” in Python, the terms “type” and “class” are synonyms. And “type” is the only class which is an instance of itself. This object model can be useful when we want information about a particular resource in Python. Except for the Python keywords like “if def, globals”, using type(<name>) or dir(<name>) or just type the resource name and press enter- will work on pretty much anything. Let’s make it clear what this statement means that “Everything in Python is an object”.

Consider the following:

As we know it very well that Python has types; however, the types are linked not to the variable names but to the object themselves. Earlier in object-oriented programming languages like Python, an object is an entity that contains data along with associated metadata and/or functionality. In Python everything is an object, which means every entity has some metadata called “attributes” and associated functionality called “methods”. These attributes and methods are accessed via the dot syntax.

For example, before we saw that lists have an append method, which adds an item to the list, and is accessed via the dot (“.”) syntax:

While it might be expected for compound objects like lists to have attributes and methods, what is sometimes unexpected is that in Python even simple types have attached attributes and methods.

For example, numerical types have a real and image attribute that returns the real and imaginary part of the value, if viewed as a complex number:

Methods are like attributes, except they are functions that you can call using opening and closing parentheses. For example, floating point numbers have a method called is_integer that checks whether the value is an integer:

When we say that everything in Python is an object, we really mean that everything is an object – even the attributes and methods of objects are themselves objects with their own type information:

Implementation through C programming language:-

Let’s now dive in the C implementation to see how are objects represented.

Those objects are manipulated under the hood as a C structure called PyObject. Ironically, the CPython object model is implemented using C, a language which is not object-oriented. From there we will notice the two following attributes:

  • Firstly a reference count, which keeps track of how many other objects and variables reference it. This is changed in the C code through the macros Py_INCREF() and Py_DECREF().
  • Secondly, a type (PyTypeObject structure), which allows Python to determine the type or class of the object at runtime. That type contains various methods which are used to describe the behaviour of the class. What function to call to allocate the type, to deallocate the type, to cast as a number, etc.

Built-in Class and User Class:

Python comes with some built-in classes, such as int, str, list, but also function or class. Contrary to a language such as Ruby where everything is also an object, Python does not allow to add new attributes or methods to the built-in types such as int or str.

The declarations of these objects are in the Include directory, and we can find in Object the various implementations of several types:

int (Objects/longobject.c), str (Objects/unicodeobject.c), list (Objects/listobject.c), user-defined classes (Objects/classobject.c), functions (Objects/funcobject.c), etc.

Each of those files defines a PyTypeObject instance which represents the type. Each PyTypeObject instance contains mostly functions that describe the behavior of the type.

For example, tp_getattro and tp_setattro, when defined, are the functions that allow to respectively read and assign a value to an attribute. The absence of tp_setattro for the “int” type explains why it is not possible to add or change an attribute to an integer. tp_as_sequence and tp_as_mapping point to lists of methods to handle standard functions for respectively functions and dictionaries.

When the program is defining a user class, the runtime creates a new type for that class.

Conclusion:

After discussing a lot about how everything in python is an object? We conclude that actually object-oriented programming is a way of thinking, it’s a method to model our code in reality and a lot easier to read, Objects are a pretty intuitive way of grouping things together that are similar, by defining a template of the generalized concept of that thing and its associated attributes and methods. So an object is an abstraction that allows us to program without having to manually keep track of every little thing.

Objects have two purposes as firstly it holds data, and it has functions to perform some works, these function may or may not be use and/or change the data of the object. So we will clearly understand that “Everything in Python is an object” is only a phrase that makes sense when we switch our learning of programming languages from C language to Python programming language and if someone is a beginner and start learning from python itself then it is always should be in mind because in Python data and appropriate functions are mixed up together in objects.

We can understand it by taking an example of Numbers as a number in C is called as primitive. But in Python, a number is an object as it may be manipulated in various ways as that number may be ‘Prime Number’ or ‘Even Number’ and we give them some specific functions and properties.

To read more about Python, click here.

By Mayank Mishra