Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'operator': 0.03; '(especially': 0.07; '22,': 0.09; 'abstraction': 0.09; 'subject:test': 0.09; 'thx': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'ast': 0.16; 'cc:name:python list': 0.16; 'debugger.': 0.16; 'evaluates': 0.16; 'tends': 0.16; 'ternary': 0.16; 'language': 0.16; 'wrote:': 0.18; 'wed,': 0.18; "python's": 0.19; 'cc:addr:python.org': 0.22; 'tend': 0.24; 'cc:2**0': 0.24; 'suggested': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'usually': 0.31; 'values.': 0.31; 'probably': 0.32; 'candidate': 0.34; "i'd": 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'shows': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'little': 0.38; 'itself': 0.39; 'even': 0.60; 'most': 0.60; 'introduced': 0.61; "you've": 0.63; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'results': 0.69; 'evaluate': 0.72; '2.5.': 0.84; 'dry': 0.84; 'subject::': 0.85; 'avoided.': 0.91; 'not:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=0UkSgBfl+Y/PlcxxCWGog3W1/aq3r1PW0CSOkbFXgqw=; b=l5wRDeOh6iSrtqTHkBGfIxqrH5EjDYDG0gTSfE2UuW8p+6maIq7E15oQtFMjIRI2zB 7mGUp7xGgCrQVEIhG8bCjusfr1rG2BeZqPdK/GMjaZFa4XVsl1zbSYInfUqPm5GX6TyT nAREjLBl8F+vIdYsWXwmg4LQMTk97wS2m9vOr+fxaXDmZ147b+V1NquxrGYa9ulsBA6K I1EFbBeYllBYRmhLgNRVg+vM++tpS7hJRkD7Pep0Q61ZegfQL9aK/a0jzNpYJ0LGBE3g NqR7xHKZc5H0zx5/a9gJ5R8ilZYKcC/ujn5bbqFPjEVRX/N7o6FYC3+I5Rk3RZtrXQhW a60w== MIME-Version: 1.0 X-Received: by 10.152.37.136 with SMTP id y8mr13315684laj.69.1413994171642; Wed, 22 Oct 2014 09:09:31 -0700 (PDT) In-Reply-To: <7839376e-fc27-4299-ae63-4ddf17ef9a8a@googlegroups.com> References: <54476af8$0$21651$426a74cc@news.free.fr> <7839376e-fc27-4299-ae63-4ddf17ef9a8a@googlegroups.com> Date: Wed, 22 Oct 2014 09:09:31 -0700 Subject: Re: (test) ? a:b From: Dan Stromberg To: buscacio@gmail.com Content-Type: text/plain; charset=UTF-8 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413994178 news.xs4all.nl 2904 [2001:888:2000:d::a6]:46443 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79453 On Wed, Oct 22, 2014 at 2:05 AM, wrote: > Em quarta-feira, 22 de outubro de 2014 06h29min55s UTC-2, ast escreveu: >> Hello >> >> >> >> Is there in Python something like: >> >> >> >> j = (j >= 10) ? 3 : j+1; >> >> >> >> as in C language ? >> >> >> >> thx > > without not: > j = [j+1, 3][j>=10] > with not: > j = [3, j+1][not (j>=10)] This is not very readable, and eagerly evaluates the two list values. A proper ternary operator will not evaluate the unused candidate-result. This matters a little for performance, but matters more if one or both of the candidate results have side-effects. It's better to use "j = 3 if j >= 10 else j + 1". What you've suggested here was one of the ternary operator workarounds before a true ternary operator was introduced in 2.5. I don't use Python's ternary operator much though - I tend to find if+else more clear, and it shows up well in a debugger. The ternary operator tends to lead to long one-liners (especially if nested!), which are usually best avoided. I'd even go so far as to say that most uses of a ternary operator probably should be a small function. It's slower, but it lends itself to DRY and abstraction better.