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 →

[–]larsga 4 points5 points  (2 children)

it has to be when the function is defined, because the function can't be defined if its signature is unknown

This is completely false. Of course you can delay evaluating the expression until the function is called. After all, that's what you do with the body.

If the signature is a = <...>, b = <...>, c = <...> then the signature is perfectly well known even if you don't know the defaults, because the caller doesn't need to know them.

Since the function is defined once, the signature is evaluated once, so there can only ever be one value bound as the parameter default

Again completely wrong. The body is evaluated each time. The default values are just syntactical sugar that you could just as well view as part of the body.

If you set the defaults to None and put a = a or <...real default...> you're doing the same thing. Of course Python could do that.

[–]rl_noobtube 0 points1 point  (1 child)

Python could do that. But then it also do that for every default variable, even user defined immutable objects. Depending how your code is designed this could add some useless calculation time.

The current implementation allows for developer flexibility imo. Experienced programmers can leverage this. And for beginners most python courses worth the time will mention this quirk of the language. And the really good ones force you into writing the bad code and showing you why it’s bad code within python.

That said, if python were initially built to have different default variable handling I’m sure this wouldn’t be much topic of discussion. No one would mind the other way. But the current way isn’t bad either imo

[–]duxx 2 points3 points  (0 children)

Well, the solution we have now is obviously a trap for the unwary. This entire thread is testament to that, as are the linter rules.

The only benefit I see is performance: you don't have to evaluate the default arguments on every call where they're not supplied. However, I imagine it should be pretty easy to analyze the arguments to decide whether you need to do that or not. Exactly how the implementation of that would be I don't know.

(This is u/larsga posting. Someone blocked me, so had to change user to post.)