Argument Presence Checking via Identity or Boolean Operation?

Russell Brennan RussellJBrennan at gmail.com
Wed Jun 3 21:18:10 EDT 2015


I'm going to x-post this to stackoverflow but...

When checking a method's arguments to see whether they were set, is it
pythonic to do an identity check:

def doThis(arg1, arg2=None):
  if arg2 is None:
    arg2 = myClass()


Or is it proper form to use a short-circuiting boolean:

def doThis(arg1, arg2=None):
    arg2 = arg2 or myClass()


In support of the former, PEP 8 states:

Comparisons to singletons like None should always be done with is or is not
, never the equality operators. Also, beware of writing if x when you
really mean if x is not None -- e.g. when testing whether a variable or
argument that defaults to None was set to some other value. The other value
might have a type (such as a container) that could be false in a boolean
context!


On the other hand, from the Google style guide:

Use the "implicit" false if at all possible. ...


But at the same time states...

Never use == or != to compare singletons like None. Use is or is not.


Does this apply to "None" since it evaluates to False always, and/or is a
boolean comparison equivalent to ==/!= under the hood?

Thanks much,
Russ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150603/a5b46e1c/attachment.html>


More information about the Python-list mailing list