Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attempting': 0.04; 'does.': 0.07; 'ignored': 0.07; 'raises': 0.07; 'python': 0.08; 'be:': 0.09; 'bool': 0.09; 'none.': 0.09; 'str': 0.09; 'subject:removing': 0.09; 'syntax.': 0.09; 'that).': 0.09; 'am,': 0.13; 'wrote:': 0.15; '"/"': 0.16; '"build': 0.16; '"can\'t': 0.16; '"cannot': 0.16; '(meaning': 0.16; '2.6,': 0.16; '3.2,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nesting': 0.16; 'none"': 0.16; 'try/except': 0.16; 'argument': 0.16; 'converting': 0.16; 'this:': 0.16; "wouldn't": 0.17; 'received:209.85.210.174': 0.19; 'received:mail- iy0-f174.google.com': 0.19; 'simpler': 0.19; 'convert': 0.19; 'appears': 0.21; 'maybe': 0.22; 'header:In-Reply-To:1': 0.22; '2.4': 0.23; 'elegant': 0.23; 'faster,': 0.23; 'specified': 0.25; 'string': 0.26; 'function': 0.26; '(and': 0.27; "i'm": 0.27; 'putting': 0.28; 'checking': 0.28; 'sat,': 0.28; 'message- id:@mail.gmail.com': 0.28; 'explicitly': 0.29; 'problem': 0.29; 'object': 0.30; 'logic': 0.30; 'typeerror:': 0.30; 'version': 0.30; 'error': 0.31; "won't": 0.32; 'chris': 0.32; 'list': 0.32; 'to:addr:python-list': 0.34; 'instead': 0.34; 'there': 0.34; 'none': 0.35; 'try:': 0.35; 'probably': 0.35; 'bother': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'two': 0.38; 'case,': 0.38; 'easier': 0.39; 'case': 0.39; 'except': 0.39; 'to:addr:python.org': 0.39; 'might': 0.39; 'received:209': 0.40; "i'd": 0.40; 'your': 0.60; 'stop': 0.61; 'back': 0.63; 'profile': 0.71; '30,': 0.74; 'hand,': 0.76; "'nonetype'": 0.84; 'concatenate': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=RWyAbZDTslr8n+OeUTMPHuvd7Th+DfFuImTeOqSpIgk=; b=ubTYg9bNU5lcIIefDIok5iIfy4kMWuRR8wLQbhaWDxJUWZocGoIQLFJcj8gzV2f7Fo IkSPAtBmQj93nb/uc/9g+aiQ4PNyoI/2c/PEW9m3dS3p2nCSW8CUblRugZFoIyKM/SD8 8zheyi9loHRwCc34zcGDfJ/RatZESvObx+x14= MIME-Version: 1.0 In-Reply-To: References: Date: Sat, 30 Jul 2011 06:47:05 +1000 Subject: Re: removing nested iffs From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311972429 news.xs4all.nl 23970 [2001:888:2000:d::a6]:57004 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10547 On Sat, Jul 30, 2011 at 5:40 AM, Josh Benner wrote: > I'm writing a function to create a string that gets longer iff an argumen= t > is defined.=A0 In there a more elegant way than nesting all those ifs? Your logic appears to be: Proceed along a specified list and stop when you find that you don't have the corresponding information (meaning that args.build will be ignored if args.project=3D=3DNone). I'd do that with multiple return statements: src =3D args.server + "::" if not args.project: return src src +=3D args.project + "/" if not args.version: return src # etc Note that I'm converting to bool ("if not blah") instead of explicitly checking for None. If an empty string is a valid project/version/build, change this back to the "is not None" syntax. I wouldn't bother with format() when it's simply putting a string on one side or the other of something; simpler just to add two strings together (and probably faster, but profile before you pick based on that). On the other hand, if your if statements are the unusual case and maybe an error, it might be easier to do this: try: =A0src =3D args.server + "::" =A0src +=3D args.project + "/" =A0src +=3D args.version + "/" =A0src +=3D "Build " + args.build except TypeError: =A0pass return src Attempting to concatenate None to a string raises TypeError "Can't convert 'NoneType' object to str implicitly" on my Python 3.2, and on Python 2.4 and 2.6, a TypeError "cannot concatenate 'str' and 'NoneType' objects". Check what your version does. In any case, the above try/except will bail out without error when it gets a TypeError of any sort, so be sure this won't be a problem to you! Chris Angelico