Reference

Tim Chase python.list at tim.thechases.com
Mon Mar 3 22:51:12 CET 2014


On 2014-03-03 21:35, Mark Lawrence wrote:
> I'd just like to know why people are so obsessed with identities,
> I've never thought to use them in 10+ years of writing Python.  Do
> I use the KISS principle too often?

There are a couple use-cases I've encountered where "is" matters:

1) the most popular:

  if foo is None:
    do_stuff()

2) sentinels (which are effectively non-None None values)

  SENTINEL = object()

  def myfuntion(value=SENTINEL):
    if value is SENTINEL:
      do_something_parameterless()
    else:
      do_something_else(value) # allow for value=None

3) when doing recursion and you want to prevent touching the same
object multiple times, such as what happens when you do

  lst = [1,2,3]
  lst.append(lst)
  print(lst)

and it needs to recognize that the final element of "lst" is one that
it has already seen (as done by identity).


There might be some other use cases, but #1 is a good 95% of my
usage, #2 is a good 4%, and I can only think of once in my Python
career when I've needed to do what is described in #3.

-tkc








More information about the Python-list mailing list