Python References

Created with Sketch.

Python References

Summary: in this tutorial, you’ll have a good understanding of Python references and referencing counting.

Introduction to Python references

In Python, a variable is not a label of a value like you may think. Instead, A variable references an object that holds a value. In other words, variables are references.

The following example assigns a number with the value of 100 to a variable:

counter = 100

Code language: Python (python)

Behind the scene, Python creates a new integer object (int) in the memory and binds the counter variable to that memory address:

When you access the counter variable, Python looks up the object referenced by the counter and returns the value of that object:

print(counter) # 100

Code language: Python (python)

So variables are references that point to the objects in the memory.

To find the memory address of an object referenced by a variable, you pass the variable to the built-in id() function.

For example, the following returns the memory address of the integer object referenced by the counter variable:

counter = 100
print(id(counter))

Code language: Python (python)

Output:

140717671523072

Code language: Python (python)

The id() function returns the memory address of an object referenced by a variable as a base-10 number.

To convert this memory address to a hexadecimal, you use the hex() function:

counter = 100

print(id(counter))
print(hex(id(counter)))

Code language: Python (python)

Output:

140717671523072
0x7ffb62d32300

Code language: Python (python)

Reference counting

An object in the memory address can have one or more references. For example:

counter = 100

Code language: Python (python)

The integer object with the value of 100 has one reference which is the counter variable. If you assign the counter to another variable e.g., max:

counter = 100
max = counter

Code language: Python (python)

Now, both counter and max variables reference the same integer object. The integer object with value 100 has two references:

Python Reference Counting

If you assign a different value to the max variable:

max = 999

Code language: Python (python)

…the integer object with value 100 has one reference, which is the counter variable:

And the number of references of the integer object with a value of 100 will be zero if you assign a different value to the counter variable:

counter = 1

Code language: Python (python)

Once an object doesn’t have any reference, Python Memory Manager will destroy that object and reclaim the memory.

Counting references

To get the number of references of an object, you use the from_address() method of the ctypes module.

ctypes.c_long.from_address(address).value

Code language: Python (python)

To use this method, you need to pass the memory address of the object that you want to count the references. Also, the address needs to be an integer number.

The following defines a function called ref_count() that uses the from_address() method:

import ctypes

def ref_count(address):
return ctypes.c_long.from_address(address).value

Code language: Python (python)

Now, you can use a shorter ref_count() function instead of using the long syntax like above.

This example defines a list of three integers:

numbers = [1, 2, 3]

Code language: Python (python)

To get the memory address of the numbers list, you use the id() function as follows:

numbers_id = id(numbers)

Code language: Python (python)

The following shows the number of references of the list referenced by the numbers variable:

print(ref_count(numbers_id)) # 1

Code language: Python (python)

It returns one because currently only the numbers variable references the list.

This assigns the numbers variable to a new variable:

ranks = numbers

Code language: Python (python)

The number of references of the list should be two now because it is referenced by both numbers and ranks variables:

print(ref_count(numbers_id)) # 2

Code language: Python (python)

If you assign ranks variable None, the reference count of the list will reduce to one:

ranks = None
print(ref_count(numbers_id)) # 1

Code language: Python (python)

And if you assign the numbers variable None, the number of references of the list will be zero:

numbers = None
print(ref_count(numbers_id)) # 0

Code language: Python (python)

Put it all together:

import ctypes

def ref_count(address):
return ctypes.c_long.from_address(address).value

numbers = [1, 2, 3]
numbers_id = id(numbers)

print(ref_count(numbers_id)) # 1

ranks = numbers
print(ref_count(numbers_id)) # 2

ranks = None
print(ref_count(numbers_id)) # 1

numbers = None
print(ref_count(numbers_id)) # 0

Code language: Python (python)

Summary

  • Python variables are references to objects located in the memory
  • Use the id() function to get the memory address of the object referenced by a variable.

Leave a Reply

Your email address will not be published. Required fields are marked *