Mutable global state (ARCH2187)
هذا المحتوى غير متوفر بلغتك بعد.
Key python.quality.mutable-global-state · Severity warning · Category quality
Module-level mutable global state creates hidden dependencies, is unsafe under concurrent access, and makes tests fragile. Encapsulate the state inside a function or class.
Incorrect
Section titled “Incorrect”cache = {}
def get(key): if key not in cache: cache[key] = load(key) return cache[key]Correct
Section titled “Correct”class Repository: def __init__(self): self._cache = {}
def get(self, key): if key not in self._cache: self._cache[key] = load(key) return self._cache[key]Encapsulate mutable state in a class instance and inject it explicitly.