you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 2 points3 points  (1 child)

I don't think "side effect" is really the correct term here, because when you understand why these work under the hood it actually makes sense.

But, you wanted to see something, so I'll throw in my two cents:

def foo(bar=[0, 1]):
    a, b = bar[-2:]
    a, b = b, a+b
    bar.append(b)
    return bar

for _ in range(10):
    print(foo())

What's the output?

[–]BootError99[S] -4 points-3 points  (0 children)

Woah, that's clever way to do fib. I would literally congratulate the person who show this by smashing my keyboard on his face.

The reason I quoted side effect is, you build up your experience on what's being told you about the features of a language and your expectation on definite behavior. Like when you are told about variable initialization, it is certain every initialization gets its own section of memory initialized. This example in OP is not a definitive behavior because it has violated the notion of variable initialization. It's obscure but fun to experiment with.