Avoiding Global Abuse (Best Practice Warning)
Mon 10 August 2026
config = {"theme": "dark"}
def update_config(new_theme):
config["theme"] = new_theme # No global keyword needed
Score: 10
Category: chapter13_global_and_scope
config = {"theme": "dark"}
def update_config(new_theme):
config["theme"] = new_theme # No global keyword needed
Score: 10
Category: chapter13_global_and_scope
count = 10
def display():
print(count)
display() # Output: 10
10
Score: 10
Category: chapter13_global_and_scope
Read Moreclass Counter:
def __init__(self):
self.value = 0
def increment(self):
self.value += 1
counter = Counter()
counter.increment()
print(counter.value) # Output: 1
1
Score: 10
Category: chapter13_global_and_scope
Read Morex = 50
def outer():
x = 10
def inner():
global x
x = 99
inner()
print("Outer x:", x)
outer()
print("Global x:", x)
Outer x: 10
Global x: 99
Score: 10
Category: chapter13_global_and_scope
Read Morex = 100
def outer():
def inner():
global x
x = 400
inner()
outer()
print(x)
400
Score: 10
Category: chapter13_global_and_scope
Read Morestatus = "inactive"
def activate():
global status
status = "active"
def show_status():
print(status)
activate()
show_status() # Output: active
active
Score: 10
Category: chapter13_global_and_scope
Read Moremode = "light"
def toggle_mode():
global mode
if mode == "light":
mode = "dark"
else:
mode = "light"
toggle_mode()
print(mode) # Output: dark
dark
Score: 10
Category: chapter13_global_and_scope
Read Morevalue = 5
def update():
value = value + 1
Score: 15
Category: chapter13_global_and_scope
Read Morerequests = 0
def handle_request():
global requests
requests += 1
handle_request()
handle_request()
print(requests) # Output: 2
2
Score: 10
Category: chapter13_global_and_scope
Read Morecounter = 0
def increment():
global counter
counter += 1
increment()
print(counter)
Score: 10
Category: chapter13_global_and_scope
Read More