class Node:
def __init__(self, edges = set()):
self.edges = edges
def main():
foo = Node()
bar = Node()
quz = Node()
foo.edges.add(bar)
bar.edges.add(foo)
assert(foo is not bar) # assertion succeeds
assert(foo is not quz) # assertion succeeds
assert(bar is not quz) # assertion succeeds
assert(len(quz.edges) == 0) # assertion fails??
main()
spoiler
Mutable default values are shared across objects. The set in this case.
Yeah, I discovered this when a coworker wrote code like
def foo(timestamp = now())
and had fun debugging why there were a bunch of duplicate timestamps.PEP 671 would add new syntax to ease the pain, but it’s not accepted yet. It would allow for writing function definitions like one of these:
def bisect_right(a, x, lo=0, hi=>len(a), *, key=None): def connect(timeout=>default_timeout): def add_item(item, target=>[]): def format_time(fmt, time_t=>time.time()):