Why this matters
Global mutable state can lead to unpredictable behavior, making debugging and testing more difficult. Functions should use local variables or class attributes instead.
Identify global variables that are mutable (e.g., lists, dictionaries). Global mutable state can cause unpredictable behavior. Recommend using local variables or encapsulating state in a class.
Global mutable state can lead to unpredictable behavior, making debugging and testing more difficult. Functions should use local variables or class attributes instead.
Side-by-side examples engineers can pattern-match during review.
global_state = {}
def update_state(key, value):
global global_state
global_state[key] = valueclass GlobalState:
def __init__(self):
self.state = {}
def update_state(self, key, value):
self.state[key] = value
global_state = GlobalState()
global_state.update_state("key", "
global_state = {}
def update_state(key, value):
global global_state
global_state[key] = valueclass GlobalState:
def __init__(self):
self.state = {}
def update_state(self, key, value):
self.state[key] = value
global_state = GlobalState()
global_state.update_state("key", "
From the same buckets as this rule.