3 ms·
It is correct, it's one of the most surprising things about Python and it causes a number of mistakes, even for experts. The easiest way to see this is by runn
by MrJohz 2y ago
It is correct, it's one of the most surprising things about Python and it causes a number of mistakes, even for experts.
The easiest way to see this is by running something like this and seeing what gets printed out and when:
print("1. start")
def function(arg=print("2. func definition")):
print("4. func call")
print("3. after definition")
function()
function()
function()
You should see that the print statement in the default position is called once, when the function definition itself is being evaluated, not when the function gets called.
- fragmede 2y agoMy canonical example is that you want a function to have an argument that defaults to an empty list: def func(arg=[]): arg.append(1) print(arg) func() func() func() shoving a print into a function definition is weird and not something you'd do normally. But someone who doesn't know this footgun is going to write a function that defaults to an empty list, and then tear their hair out when things are broken.
- deleted 2y ago[deleted]
- MrJohz 2y agoThat's true, this is the example that most people will run into in practice. But the print example is useful because it shows what's going on more explicitly: the default argument gets evaluated as part of the function's definition. I think this helps people get a better intuition for how Python's interpreter works when it comes to evaluating function definitions.
- OptionOfT 2y agoAnd because it is evaluated once, unlike in say TypeScript, when you modify `arg` in the first call, the results are visible in the second call.
- johnny22 2y agoI find this fascinating because while I don't write much python, this is the behaviour I would assume to be correct based on everything else I've seen. I wouldn't expect it to be evaluated on every call. Definitely shows that your previous familiarity can trip you up (or not)
- raverbashing 2y ago> it's one of the most surprising things about Python and it causes a number of mistakes, even for experts. Except every python 101 text seems to go over it, and people seem to have suddenly forgotten about it ChatGPT driven development maybe?
- MrJohz 2y agoThis has been a source of bugs since long before ChatGPT. I suspect Python tutorials mention it so often because it's such a surprising feature and causes so much confusion. I've been using Python for fifteen years, and it's the sort of thing that I'll still absent-mindedly forget if I'm not careful.
- int_19h 2y agoIronically, in C++ - which does it the other way around - the tutorials mention that often because it is also such a surprising feature (i.e. people do not expect evaluation to occur every time there). Thing is, there's no obviously correct behavior here, and there are valid arguments to be made either way. Which is why many languages dodge the bullet by only allowing for compile-time constants as defaults in that context (and if you want to evaluate something at runtime, you can always use an optional and do an explicit check inside the body, or provide an overload).