Python static variable in a function: the equivalents
Codemia Knowledge Hub ·
Python has no static local variables like C or C++, but you can keep state between calls in four ways: store it as an attribute on the function, capture it in a closure with nonlocal, wrap it in a class, or rely on a mutable default argument. Each avoids a global variable.
Function attributes
Functions are first-class objects in Python, so you can attach attributes to them. This is the most direct imitation of a static variable:
counter.count keeps its value between calls, so it counts how many times counter() has run.
Mutable default arguments
Default argument values are evaluated once, when the function is defined. If the default is mutable and you modify it, the change persists across calls:
This often causes bugs when a function was not meant to share state between calls, so use it deliberately or not at all.
Closures with nonlocal
A nested function can reference variables from its enclosing scope, and nonlocal lets it modify them:
Each call to my_counter() increments and returns count, which stays hidden from the global scope.
A class
A class is more verbose but gives the most control, especially when several related values need to persist:
Choosing a technique
| Technique | Use Case | Pros | Cons |
| Function Attributes | Simple function-level static variables | Straightforward | Not thread-safe |
| Mutable Default Arguments | Simple use cases, default caching | Easy to use; less code | Can lead to unexpected bugs |
| Closures and Nonlocal | Complex stateful functions, encapsulation | Good encapsulation; clear and clean | Slightly more complex |
| Class-Based | Complex state, multiple related static variables | Full control, encapsulation | More verbose; might be overkill for simplicity |
The right choice depends on whether you value simplicity, encapsulation or flexibility more for the code at hand.
Related reading
- What is the python keyword with used for?
- What is the Python with statement designed for?
- What is the pythonic way to detect the last element in a 'for' loop?
- What is the quickest way to HTTP GET in Python?
- What is the reason for having '//' in Python?
- What is the recommended way to use Vim folding for Python code?
- What is the relationship between virtualenv and pyenv?
- What is the result of modulo operator / percent sign in Python?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.