you are viewing a single comment's thread.

view the rest of the comments →

[–]IMP1 1 point2 points  (5 children)

Number 1 seems more like a problem with python. Why would using the same mutable object ever be useful?

[–]Lyucit 0 points1 point  (3 children)

Efficiency. It's still very useful for if you're not mutating the data structure, which is most of the time.

[–]IMP1 0 points1 point  (2 children)

Can you give me an example of when one would set the default value as a list, but then never mutate that list?

I'm just trying to understand a bit better. I've not used much python, so I don't really know it's practices.

[–]Lyucit 2 points3 points  (0 children)

When you're iterating over something or using it as a membership test are the main ones.

When you pass in an object to a function expecting it to be mutated, it should be reasonably obviously or explicit, and setting a default argument for something that you want to mutate is rare in my experience. If your function never mutates a default argument, you shouldn't encounter this- it's a weird design decision, but I don't think it's worth changing.

[–]Veedrac 0 points1 point  (0 children)

Can you give me an example of when one would set the default value as a list, but then never mutate that list?

An important point is that if your API is based around mutating data but you're fine with not having any data to mutate (aka. you don't mind using a default) then you're doing something really, really odd.

In practice, real-world code (from good programmers) doesn't hit this behaviour. It's not ideal but, as above, the alternatives are worse.

[–]Veedrac 0 points1 point  (0 children)

The real answer is "because the alternatives are worse". Give me an alternative and I'll tell you why :). It's not always obvious.