← Back to context

Comment by Spivak

5 months ago

Python made a choice to have default values instead of default expressions and it comes with positive and negative trade-offs. In languages like Ruby with default expressions you get the footgun the other way where calling a function with a default parameter can trigger side effects. This kind of function is fine in Python because it's unidiomatic to mutate your parameters, you do obj.mutate() not mutate(obj).

So while it's a footgun you will be writing some weird code to actually trigger it.

>In languages like Ruby with default expressions you get the footgun the other way where calling a function with a default parameter can trigger side effects.

Seems fine to me. If the default expression causes side effects, then that's what I would expect.

>This kind of function is fine in Python because it's unidiomatic to mutate your parameters, you do obj.mutate() not mutate(obj).

I first wrote Python over 10 years ago and I never learned this.

How would you idiomatically write a function/method which mutates >1 parameter?

  • It's just plain wrong. For example, next() is a builtin function which mutates the iterator passed to it. And, in general, given that Python doesn't have extension methods or anything similar, if you want to write a helper that works on mutable objects of some type, it'll have to be a free function.

    • next() is just sugar for iter.__next__()

      A common case where you would have a free function which mutates its parameter would be a function which takes a file handle but it's also the case that you wouldn't have a mutable default for this value.

      1 reply →

  • They are referring to a convention, not a language restriction.

    If you want to mutate two parameters just pass them to a function like you normally would.

    It's sloppy and a bad habit, I would not let it pass a PR in production code. Probably OK for a throwaway script.