Skip to content

Understanding Variable Lifetimes in Python

Updated: at 05:45 AM

In Python, variables are created and destroyed automatically based on their scope and lifetime. Knowing when a variable comes into existence and when it gets deleted from memory is crucial for writing optimized Python programs. This article will provide a comprehensive guide on variable lifetimes in Python, explaining when variables are created, how long they last, and when they are destroyed.

Table of Contents

Open Table of Contents

Introduction

Python uses a variety of techniques like reference counting and garbage collection to efficiently allocate and free up memory used by variables. The lifetime of a Python variable is the period of time during which the variable exists and holds a value in memory. After it is no longer needed, Python can free up the memory allocated to that variable.

Understanding variable lifetimes in Python will help you:

Variable Scope in Python

The scope of a variable is the part of the code where that variable is accessible. Python has the following variable scopes:

Example of Variable Scope

# Global variable
my_var = "foo"

def my_func():
  # Local variable
  my_var = "bar"

  print(my_var)

my_func() # Prints "bar"

print(my_var) # Prints "foo"

Here my_var inside the function is local while the outer my_var is global.

When Are Variables Created in Python?

Python variables come into existence or get created at different times depending on their scope:

Local Variables

def my_func():
  print(my_var) # None
  my_var = "foo"

my_func()

Global Variables

# main.py

print(my_var) # None

my_var = "foo"

Function Parameters

def my_func(param1, param2):
  print(param1) # "foo"

my_func("foo", "bar")

Instance Variables

class MyClass:
  def __init__(self, x=5):
    self.x = x

obj = MyClass(10)
print(obj.x) # 10

Class Variables

class MyClass:
  class_var = 5

print(MyClass.class_var) # 5

Module Level Variables

# mymodule.py

foo = 10
# main.py
import mymodule

print(mymodule.foo) # 10

Temporary Variables

Some variables in Python are temporary and short-lived:

for i in range(5):
  print(i)

print(i) # Error, i doesn't exist here
vals = [x*2 for x in range(5)]
print(x) # Error

These temporary variables are deleted after the loop or comprehension finishes execution.

When Are Variables Destroyed in Python?

Python automatically destroys variables when they go out of scope or are no longer used by the program. This frees up the occupied memory.

Reference Counting

Python uses reference counting to manage memory for some objects like strings, tuples, etc. When the reference count reaches zero, the object is immediately destroyed.

my_str = "foo"
print(my_str)

# Reference count decreased to 0, string destroyed
my_str = "bar"

Garbage Collection

For objects with cycles like lists, dicts, or custom classes, Python cannot rely on reference counting alone. It uses a cyclic garbage collector which periodically looks for and cleans up unused objects in cycles.

del Statement

We can explicitly delete variables before their end of scope using the del statement. This directly removes the reference and destroys the object.

my_list = [1, 2, 3]

del my_list

print(my_list) # Error, my_list was deleted

Rebinding Variables

When a variable is reassigned to a new object, the old object’s reference count decreases. If no other references exist, it gets destroyed.

my_list = [1, 2, 3]

# Old list object destroyed if no other references
my_list = ["a", "b", "c"]

Best Practices for Variable Lifetimes

Here are some tips for working with variable lifetimes in Python:

Conclusion

Understanding the creation and destruction of variables is an important part of writing optimized Python code. By properly managing variable lifetimes, we can build Python programs that efficiently utilize memory resources.

In this guide, we looked at how variables come into existence and when they go out of scope in Python. We covered techniques like reference counting and garbage collection used by Python to clean up unreferenced objects and free up occupied memory automatically. We also discussed best practices to balance automatic and manual memory management in your code.

Applying these concepts properly will allow you to make your Python programs faster, minimize memory consumption, and prevent bugs arising due to invalid references to dead variables.