Solved The evening record contains the morning reading?! EMEmmaCodesOP 7 Oct, 22:18 Stage 1 prediction was so wrong. How can two separate calls share a list?
LILiamByte8 Oct, 07:51 Solutionreplies: 2Default values are evaluated once, when the function is defined — not on each call. So flags=[] creates one list that every call without flags reuses. Append to it once and every later call sees that item.
FRFreyaL8 Oct, 17:10Every Python developer hits this once. Linters (ruff, pylint) flag mutable defaults automatically.
HEHenryWrites8 Oct, 12:53The standard fix is in stage 3: default to None and create a new list inside the function if it's None.
Comments 5
Join the discussion CommentAlready have an account? Log in
Default values are evaluated once, when the function is defined — not on each call. So
flags=[]creates one list that every call withoutflagsreuses. Append to it once and every later call sees that item.Defined once, reused forever. Wild.
Every Python developer hits this once. Linters (ruff, pylint) flag mutable defaults automatically.
The standard fix is in stage 3: default to
Noneand create a new list inside the function if it's None.This is the one I always mention when people say Python has no gotchas 😄