What is the Python equivalent of static variables inside a function?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python does not have a direct static keyword for function-local variables the way C or C++ does. Even so, Python offers several ways to preserve state across function calls. The right choice depends on whether you want the state to be visible, private, or split across multiple independent instances.
Function Attributes Are the Closest Match
Functions are regular objects in Python, so you can attach attributes to them. That makes function attributes the closest conceptual equivalent to a static variable tied to one function.
The state lives on the function object itself. That makes the behavior easy to inspect in a debugger and easy to reset in tests.
Closures Keep the State Private
If you want persistent state but do not want to expose it as a public attribute, a closure is usually cleaner.
Each call to make_counter() creates a new private state cell. That makes closures a better fit when you need multiple independent counters rather than one global function-level counter.
Classes Are Often the Clearest Design
Once the preserved state grows beyond a trivial counter or cache, a class is often the most readable solution.
A class makes the state explicit, testable, and extensible. If you anticipate more behavior later, starting with a class is often simpler than hiding state inside a function trick.
Mutable Default Arguments Can Persist State
Default argument expressions are evaluated once, so a mutable default can also behave like persistent state.
This works, but it is easy to misread. Many Python programmers associate mutable defaults with bugs because accidental state retention is a common mistake. Use this pattern only when the persistence is intentional and obvious to readers.
Use Standard Tools for Common Stateful Patterns
Sometimes what looks like a static variable problem is really a caching problem. In those cases, the standard library often provides a clearer answer.
This is usually better than manually storing cache data on the function when the real goal is memoization.
Common Pitfalls
The biggest mistake is hiding shared mutable state where readers do not expect it. A function that looks pure but remembers old calls can surprise other developers and make tests interdependent.
Another issue is choosing a clever function-level technique when a small class would be clearer. If the state has more than one field or needs reset logic, explicit objects usually win.
Mutable default arguments are especially risky because they are easy to use accidentally. If you use them on purpose, make that intent obvious in the function name, documentation, or surrounding code.
Finally, remember that all of these patterns create shared state unless you deliberately create separate instances. In concurrent code, that can introduce race conditions or hidden coupling between callers.
Summary
- Python has no direct function-local
statickeyword. - Function attributes are the closest simple equivalent.
- Closures are better when you want private state or multiple independent instances.
- Classes are usually the clearest choice once the state becomes non-trivial.
- Use purpose-built tools such as
lru_cachewhen the real need is caching rather than generic persistence.
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?
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.