Every story and comment from the Hacker News API, in a layout borrowed from 2001.
A comment on Why Do Python Lists Multiply Oddly? Exploring the CPython Source Code. Parent comment
The code I posted is showing how you can explicitly get the mutable behavior if you want it when the default expressions are evaluated at call time.
That is to say, if you evaluate default expressions at call time, you can get either behavior by being explicit with minimal loss in performance:
def foo(xs = []): xs.append(1) print(xs) # always prints [1] DEFAULT = [] def bar(xs = DEFAULT): xs.append(1) print(xs) # prints [1], [1,1], [1,1,1], etc.
You're clearly not as knowledgeable as you think you are on this and you're just cherry picking things you don't understand to feel smart.
There are no comments on this comment yet.
The code I posted is showing how you can explicitly get the mutable behavior if you want it when the default expressions are evaluated at call time.
That is to say, if you evaluate default expressions at call time, you can get either behavior by being explicit with minimal loss in performance:
To reiterate, the above code is what would happen if default expressions were evaluated at function call time.You're clearly not as knowledgeable as you think you are on this and you're just cherry picking things you don't understand to feel smart.