Object Model in Python - Understanding Internals

Object Model in Python - Understanding Internals

Object Model in Python - Understanding Internals  Awantik Das
Posted on May 22, 2017, 12:49 p.m.

object model in python

The object model of Python is something very less discussed but important to understand what happens under the cover. Understanding this before diving into python makes journey smooth

1. >>a = 5, What this line does is, it creates an object which holds value 5 & a refers to it.

2. Each Object consist of 3 things - data ( 5 in this case ), type ( int in this case ) & reference_count ( 1 in this case)

3. If we allocate numbers below 255 or string of smaller size ( not separated by special characters ), Python Object Model shares the object by increasing the reference_count.

4. If two references a & b refers to same objects, id(a) == id(b) will evaluate to be true. Another way to test this is a is b.

5. a = 5, b = 5. Reference count of the object containing 5 is 2 now.

6. a = 6. Reference count of object containing 5 is one now

7. b = 7. Reference count of object containing 5 is zero now.

8. Objects are eligible for memory cleanup when their reference count is zero. Periodic Garbage collection will reclaim the memory 

9. One important point to note here is, numbers above 255 & longs strings are not shared because chances of them using in code is far less. Doing a lookup everytime will impact CPU utilization every time a variable is assigned. This provides a eqilibrium in memory vs CPU utilization.

10. Because of the type information associated with every object, 5 & 5.0 will occupy different piece of memory. And, hense different objects.


Awantik Das is a Technology Evangelist and is currently working as a Corporate Trainer. He has already trained more than 3000+ Professionals from Fortune 500 companies that include companies like Cognizant, Mindtree, HappiestMinds, CISCO and Others. He is also involved in Talent Acquisition Consulting for leading Companies on niche Technologies. Previously he has worked with Technology Companies like CISCO, Juniper and Rancore (A Reliance Group Company)




Keywords : Python


Recommended Reading


How should I start learning Python?

Python is a powerful, flexible, open source language that is easy to learn, easy to use, and has powerful libraries for data manipulation and analysis. Its simple syntax is very accessible to programming novices, and will look familiar to anyone with experi...


How do I check end of file (EOF) in python?

Almost every programming language provides a straightforward mechanism to check the End of File. Python however does not have a very straightforward and intuitive way to check EOF. However following piece of code can help you find EOF for all practical purp...


Deep Dive into Understanding Functions in Python

Python provides very easy-to-use syntaxes so that even a novice programmer can learn Python and start delivering quality codes, yet it does not compromises on the powerful features that it provides. With respect to functions it gives a lot of flexibility to...