Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92039 > unrolled thread

Re: Argument Presence Checking via Identity or Boolean Operation?

Started byPeter Otten <__peter__@web.de>
First post2015-06-04 10:07 +0200
Last post2015-06-04 10:07 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Argument Presence Checking via Identity or Boolean Operation? Peter Otten <__peter__@web.de> - 2015-06-04 10:07 +0200

#92039 — Re: Argument Presence Checking via Identity or Boolean Operation?

FromPeter Otten <__peter__@web.de>
Date2015-06-04 10:07 +0200
SubjectRe: Argument Presence Checking via Identity or Boolean Operation?
Message-ID<mailman.152.1433405311.13271.python-list@python.org>
Russell Brennan wrote:

> 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()

When I read this I always have to stop and consider whether there are valid 
falsey arguments that could be passed as arg2.

An obvious example is

def do_this(arg=None):
    arg = arg or []
    arg.append(42)

a = []
do_this(a)
do_this(a)
print(a) # expected: [42, 42] actual output: []

Choosing the first approach is not just a matter of style, it avoids a 
source of subtle bugs.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web