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


Groups > comp.lang.python > #92040

Re: Argument Presence Checking via Identity or Boolean Operation?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'none:': 0.05; 'arg': 0.09; 'comment,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subtle': 0.09; 'def': 0.14; 'argument': 0.15; 'bugs.': 0.16; "method's": 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'set,': 0.16; 'wrote:': 0.16; 'arguments': 0.22; 'pass': 0.22; 'wrote': 0.23; 'example': 0.25; 'header:X-Complaints-To:1': 0.26; 'checking': 0.27; 'actual': 0.29; "i'm": 0.29; 'source': 0.31; 'skip:_ 10': 0.32; 'choosing': 0.33; 'subject:?': 0.34; 'changed': 0.35; 'could': 0.35; 'to:addr :python-list': 0.35; 'identity': 0.35; 'there': 0.36; 'subject:: ': 0.37; 'received:org': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'matter': 0.63; 'frank': 0.72; 'obvious': 0.72; 'russell': 0.84; 'subject:via': 0.84; 'variation': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From "Frank Millman" <frank@chagford.com>
Subject Re: Argument Presence Checking via Identity or Boolean Operation?
Date Thu, 4 Jun 2015 10:34:28 +0200
References <CAAKgPaGY_4yB7z=R-UO=s5TEWwEXZsE1nSEXw00sQPMfH_Xt2g@mail.gmail.com> <mkp10p$n0l$1@ger.gmane.org>
X-Gmane-NNTP-Posting-Host 197.86.223.171
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.3790.4657
X-RFC2646 Format=Flowed; Original
X-MimeOLE Produced By Microsoft MimeOLE V6.00.3790.4913
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.153.1433406882.13271.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433406882 news.xs4all.nl 2945 [2001:888:2000:d::a6]:47231
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92040

Show key headers only | View raw


"Peter Otten" <__peter__@web.de> wrote in message 
news:mkp10p$n0l$1@ger.gmane.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.
>

I have a slight variation in that I want to keep a reference to the 
argument -

def __init__(self, arg=None):
    self.arg = arg or []

Based on your comment, I have changed it to -

def __init__(self, arg=None):
    self.arg = [] if arg is None else arg

Does this pass your 'stop and consider' test?

Frank Millman


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Argument Presence Checking via Identity or Boolean Operation? "Frank Millman" <frank@chagford.com> - 2015-06-04 10:34 +0200

csiph-web