← Lesson
Scope, Defaults, and Extra Arguments
Joeven
Run
Reset
Python loads on first run
def add_event(event, history=None): if history is None: history = [] history.append(event) return history print("good", add_event("a")) print("good", add_event("b")) def bad_add(item, bag=[]): bag.append(item) return bag print("bad", bad_add("x")) print("bad", bad_add("y")) def log_all(*parts, **fields): print("extra positional", parts) print("extra named", fields) log_all("tool", "search", ok=True, ms=12) score = 10 def bump(): score = 1 print("inside function, score is", score) bump() print("outside function, score is still", score)
Run to execute this in your browser. Nothing is sent to a server.