Are variables memory references in python?
All about variables and memory references in python
Let’s discuss a simple real-world example before jumping onto the actual part on variables and memory references in python.
Co-related Example
We normally use emails every day, whether at work or at college for academic purposes. To send an email to someone, we require an address, such as (receiver@gmail.com). This address refers to a specific mailbox somewhere on the globe. Using this address, we can ensure that the email’s content is delivered to the recipient’s inbox.
Memory
A computer’s memory can be assumed as a series of blocks that exists inside a computer, to store and retrieve data from these blocks.
The above-mentioned email example can be co-related to the computer’s memory understanding. The computer needs a unique address for each of these blocks and the computer’s memory address is typically represented in the form of numbers. [0x2000,0x2001,0x2002…] are some arbitrary hexadecimal numbers representing the memory address.
How it stores?
At times, storing the data in memory addresses might require more than one block at a time.
For instance, each block in the memory is of 2 bytes and object_A is of size 4 bytes and this object is stored in memory at address 0x2000 and overflown to 0x2001. Similarly, if object_B requires 5 bytes, it started at address 0x2002 and occupied till 0x2004 and if in case, object_C requires only 2 bytes, then it would fit precisely in a single block (like at 0x2005).
The heap is a memory space that is used to allocate memory blocks for the program. Storing and retrieving objects from the heap is taken care of by the Python memory manager.
Inner crux
When a line of code is written as demo_variable = 77, python creates an object in memory at some address, here it is created at 0x2001. demo_variable is like an alias for the memory address, where that object is stored.
If the variable requires only one block of memory, then it would be said that it is stored in memory address 0x2001 but, if it requires multiple blocks of memory, then 0x2001 would be regarded as starting address of the object (as shown in the above image).
demo_variable is said to be a reference to the object.
When we code, we typically assume that demo_variable is equal to 77 (logically it might be true) but the crux behind is that demo_variable is a reference to an object at that particular location. In this case, demo_variable is not equal to 77, it is equal to 0x2001. 0x2001 represents the memory address of data, we use in the code (here, it is 77).
Similarly, if I code string_variable = ‘Test’, then it will first get created and stored at some place in the memory.
string_variable becomes an alias (or) reference to the memory address 0x2022.
Pythonic way
We might not use these functions in typical python programs but it is always an additional edge, to understand what’s happening inside a python program.
id() function in python is used to find the memory address referenced by a variable.
id() function will return a base 10 number. This can be converted to a hex number by using python’s inbuilt hex function.
So, it can be clearly understood that
Variables in python are always references to the objects in memory.