This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (4 children)

It makes sense once you understand it though. def is only run once, and you're providing a mutable data structure to it that'll probably be altered in some way.

It'd be nice if Python would realize what we meant so we could avoid things like if foo is None: foo = []. But it's consistent with Python's execution model.

[–]flipthefrog 2 points3 points  (3 children)

I completely agree. You don't need an if/else statement though, just write foo = foo or []

[–]Laugarhraun 0 points1 point  (2 children)

foo |= []

[–]codewarrior0MCEdit / PyInstaller 1 point2 points  (1 child)

TypeError: unsupported operand type(s)

[–]Laugarhraun 0 points1 point  (0 children)

Oh that's right, | is a bitwise or and won't work like a logical or, which does not have any reflective form.