Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: George Trojan Newsgroups: comp.lang.python Subject: Re: tuples in conditional assignment (Ben Finney) Date: Tue, 24 Nov 2015 16:40:23 +0000 Lines: 76 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de t2UO8OvUcGjKrZ+kTpyIAQTUg0WWtOlDNPimuYrxka1A== 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; 'else:': 0.03; 'operator': 0.03; 'that?': 0.05; 'assignment': 0.07; 'column': 0.07; 'expressions': 0.07; '(0,': 0.09; 'assigning': 0.09; 'assumed': 0.09; 'operator,': 0.09; 'precedence': 0.09; 'received:140': 0.09; 'rules.': 0.09; 'simplified': 0.09; 'stating': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'python': 0.10; 'assume': 0.11; 'exception': 0.13; '>>>': 0.15; 'explicitly': 0.15; '1),': 0.16; 'advice:': 0.16; 'clauses': 0.16; 'comma': 0.16; 'comma.': 0.16; 'grasp': 0.16; 'parentheses': 0.16; 'precedence.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'threw': 0.16; 'tuple,': 0.16; 'tells': 0.18; '>>>': 0.20; '(not': 0.20; '(a)': 0.22; 'assuming': 0.22; 'satisfying': 0.22; 'url:gt': 0.22; 'trying': 0.22; 'code.': 0.23; 'somewhere': 0.24; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'error': 0.27; 'least': 0.27; 'actual': 0.28; 'yields': 0.29; "i'm": 0.30; 'code': 0.30; 'mention': 0.30; 'rules': 0.31; 'date:': 0.31; 'table': 0.32; 'are:': 0.32; 'statement': 0.32; 'maybe': 0.33; 'url:python': 0.33; 'call,': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'on,': 0.35; 'too': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'email addr:python.org': 0.36; 'evaluation': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'display': 0.37; 'list.': 0.37; 'received:209': 0.38; 'names': 0.38; 'anything': 0.38; 'received:209.85.220': 0.38; 'why': 0.39; 'sure': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'url:3': 0.60; 'charset:windows-1252': 0.62; 'more': 0.63; 'complete': 0.63; 'times': 0.63; '>>>>>': 0.66; 'binding': 0.66; 'email name:python- list': 0.67; 'obvious': 0.76; '_o__)': 0.84; 'bitten': 0.84; 'case?': 0.84; 'descriptive': 0.84; 'obvious.': 0.84; 'recently:': 0.84; 'universe': 0.84; 'trojan': 0.91; 'url:reference': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=noaa-gov.20150623.gappssmtp.com; s=20150623; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; bh=788UcOUKjok/BsR7C5DBuaxBJx4xbWHWe78wWEqcdcQ=; b=RCpsq4zcxmXwnB700N0cBt6z4iiHhyLLugnjRclSRM8X25Xm07zBw/HVIIcdCkyGmz 0FO0oDkE9DafHOKGkk5jE2NB+5tbEIZ1Xre24wR10qQdbIXBNIq+s5qsrTtcHaguS+g3 uE1JQxuKEMbsKmObsjG9G/9XUNVVo6U6uIj6lKFEuWqo0IRRKNVE9qc9jROjyQ4z+kP2 s5z06s5wk5pr6fqcnjNfiG2saZFdVkthsRhHSouT339Ltsww42RHmaYae+yoVay5ldCW ebhHkix+IGIbAwveTInGNtGDUY3Lhmhp7F0fbUC2CkmqIPosabJGgiAuKGcWyJ+hBALX 2OLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type; bh=788UcOUKjok/BsR7C5DBuaxBJx4xbWHWe78wWEqcdcQ=; b=O8SHtLkQECZVSmMBzkOGj/QVVzd4X5YjIghZkfnAcMreIdAe5XThhQy20gRim6fwr+ 0irwW5HOw9S+jvb+kqbsbT3GJDHLeyOujrtFBViW/389wknYI3cA/POqZlnRE+LGm6cM nVClaOCNl1pQmEFuWt9FFGqMXIxq4N5R2FU53PYbVnoLRS/v7qkDwsfZ4UJ3H7zPOuKB caCLLvNuQvXYQ0u/Xklr/iWQ0k5yBon/BHwEoubJosKOeEYOTZxO2hi/6pjpJw7GGrM+ FB2ZpFd+rMkA8ZmQPDH3dfO8DX6RMy84KTeMy9+h53TWk0M4asF0zRvWJZbjFAhX486u ZDyA== X-Gm-Message-State: ALoCoQkQkIybz+tO3zPPZ+lbFZzKUJaLUVlzFhWsy1//8r/qLB84kwgTCSnBUyoqxgn+nCC7J2AM X-Received: by 10.66.122.67 with SMTP id lq3mr43926749pab.21.1448383224603; Tue, 24 Nov 2015 08:40:24 -0800 (PST) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99365 Ben Finney writes: > Ben Finney > Date: > 11/24/2015 04:49 AM > > To: > python-list@python.org > > > George Trojan writes: > >> The following code has bitten me recently: >> >>>>> t=(0,1) >>>>> x,y=t if t else 8, 9 >>>>> print(x, y) >> (0, 1) 9 > You can simplify this by taking assignment out of the picture:: > > >>> t = (0, 1) > >>> t if t else 8, 9 > ((0, 1), 9) > > So that's an “expression list” containing a comma. The reference for > expressions tells us:: > > An expression list containing at least one comma yields a tuple. The > length of the tuple is the number of expressions in the list. > > > >> I was assuming that a comma has the highest order of evaluation > You were? The operator precedence rules don't even mention comma as an > operator, so why would you assume that? > > What threw me off was the right column in the table stating that binding or tuple display has the highest precedence. Somewhere else there is a statement that a comma makes a tuple, not the parentheses, so the parentheses on the left did not register with me. > >> that is the expression 8, 9 should make a tuple. Why this is not the >> case? > I'm not sure why it's the case that you assumed that > > My practical advice: I don't bother trying to remember the complete > operator precedence rules. My simplified precedence rules are: > > * ‘+’, ‘-’ have the same precedence. > * ‘*’, ‘/’, ‘//’ have the same precedence. > * For anything else: Use parentheses to explicitly declare the > precedence I want. > > Related: When an expression has enough clauses that it's not *completely > obvious* what's going on, break it up by assigning some sub-parts to > temporary well-chosen descriptive names (not ‘t’). t was just to illustrate my problem, not the actual code. My lesson is to use parentheses in all cases, maybe with an exception for an obvious y, x = x, y. In my new C code I always write if (a) { f(); } instead of a valid 2-liner if (a) f(); Too many times I added indented g() call, and since I do more Python than C, the error was not glaringly obvious. > > -- \ “It is far better to grasp the universe as it really is than to | > `\ persist in delusion, however satisfying and reassuring.” —Carl | > _o__) Sagan | Ben Finney George