I just realized that <dict>.setdefault *always* executes the second
argument - even if it's not necessary, because the requested item in
the first argument exists.

This is not what I expected - why evaluate the second argument if it's
not needed? Also this can lead to side-effects!

Click to expand...

Because Python doesn't have macros or special forms that look like
functions. If you see something that looks like a function, it
evaluates all arguments before it calls the function. Always. Changing
this would cause confusion, not resolve it.

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Ask a Question