Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What realistic use case do you have for caring about whether two integers of the same value are distinct objects? Modern versions of Python warn about doing unpredicatble things with `is` exactly because you are not supposed to do those things. Valid use cases for `is` at all are rare.
 help



if v is not None as opposed to if not v is one of those use cases if you store 0 or False or an empty list, etc.

> Valid use cases for `is` at all are rare.

There might not be that many of them, depending on how you count, but they're not rare in the slightest. For example, you have to use `is` in the common case where you want the default value of a function argument to be an empty list.


I assume you refer to the `is None` idiom. That happens often enough, but I count it as exactly one use case, and I think it's usually poorly considered anyway. Again, you probably don't actually want the default value to be an empty list, because it doesn't make a lot of sense to mutate something that the caller isn't actually required to provide (unless the caller never provides it and you're just abusing the default-argument behaviour for some kind of cache).

Using, for example, `()` as a default argument, and cleaning up your logic to not do those mutations, is commonly simpler and more expressive. A lot of the community has the idea that a tuple should represent heterogeneous fixed-length data and a list should be homogeneous; but I consider (im)mutability to be a much more interesting property of types.


Could you expand on this? For example, this works just fine:

    def silly_append(item, orig=[]):
       return orig + [item]
Edit: Oh, I think you probably mean in cases where you're mutating the input list.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: