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 consists 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 an equilibrium in memory vs CPU utilization.

10. Because of the type information associated with every object, 5 & 5.0 will occupy a different piece of memory. And, hence 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. Python has a unique combination of being both a capable general-purpose programming language as well as b...

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 does not have a very straightforward and intuitive way to check EOF.Following code can help you find EOF for all practical purposes.

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.It gives a lot of flexibility to programmers to make the code more reusable, readable and compact. To know more about what are the...