Everything in Python is an Object

Ajay Bile
3 min readMar 22, 2023

--

Hey There !

In today’s blog I will be discussing below topics in brief.

1. Interpreted Vs Compiled Language
2. Python Interpreter
3. CPython
4. Structure in C language
5. PyObject

Interpreted Vs Compiled Language

Every Program is set of instructions. It means whatever code we write is ultimately going to be machine code. and then this machine code will run by processor.

In Compiled Language when we compiled program it gets converted into machine readable format. Machine can directly start processing the compiled code.

But in Interpreter language internally code gets converted into byte code by byte code compiler which is not a final machine understandable code. so to make it machine understandable there is an interpreter which convert the code into machine code line by line and hence code gets executed by machine line by line.

Python Interpreter & CPython

So Python is an Interpreted language. and this default interpreter in python is written by C language called as CPython.

So now here it is interesting to know that if python is an object oriented programming language and its interpreter is written in C language then how does this entire thing works under the hood.

Structure in C language

Because CPython is written in C, which is not an object oriented programming language, this is what makes everything in python an object.
In Python even your str type, int type under the hood everything is stored as a struct (PyObject)
A Struct, or Structure, in C is a custom data type that groups together different data types.
To Compare to Object Oriented languages, it is like a Class with attributes and no methods.

struct MyStructure { // Structure declaration
int myNum; // Member (int variable)
char myLetter; // Member (char variable)
};

PyObject

PyObject is nothing but python object which is structure in C implementation.

Now lets try to understand when we assign value 11 to x in python what its datatype and how does it behave when we changed its value.

In Python when we do x = 11, behind the scene it create a PyObject which basically stores metadata like value, type and reference count and then this name x will point to that metadata object that is PyObject.

id() — returns the objects memory address

is — returns True if and only if two objects have same memory address

>>> x = 11
>>> id(x)
2233128151664
>>> x = x + 1
>>> x
12
>>> id(x)
2233128151696
>>> y = x
>>> y is x
True
>>> x is y
True
>>>
x = 11
x = x + 1

Note that object without any reference is eligible for garbage collection.

y = x

Hope you like this blog. Follow me for more such blogs.

Thanks.

--

--

Ajay Bile

I learn everyday 😎 Python-SQL-Big Data-Data Engineering-Process Automation