Spivak 6 days 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.

1
0xDEAFBEAD 6 days ago

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

int_19h 6 days ago

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.

Spivak 6 days ago

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.

int_19h 4 days ago

It doesn't change the fact that it's a function that mutates its parameter, even if it eventually calls a method to do so. And, furthermore, it is idiomatic to call next(iter) rather than iter.__next__(), even when no default value is expected.

biorach 6 days ago

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.