News for hackers. Stuff that matters.
Re:Why Do Python Lists Multiply Oddly? Exploring the CPython... by kerkeslager on Wednesday April 10, 2024 @05:06PM (#39995730)
> You're proposing that the interpreter add a check for every default parameter in every function signature

No, that's not what I'm proposing. Why would it check anything? Just evaluate the given default expression at call time. If you don't want the overhead of an expression, don't put a default.

You can also do defaults like:

    LIST_OF_X = []

    def foo(xs = LIST_OF_X):
        ...
...if you want the other behavior. This does add a variable lookup (oh no!).
Re:Why Do Python Lists Multiply Oddly? Exploring the CPython... by klyrs on Wednesday April 10, 2024 @07:06PM (#39996607)
Your list there is mutable. Isn't that what you meant to solve with this?
Re:Why Do Python Lists Multiply Oddly? Exploring the CPython... by kerkeslager on Thursday April 11, 2024 @11:24AM (#40003197)
Ugh. You're just ignoring my entire post except the one part where you (wrongly) think you can correct me?

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.
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.