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


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

tuples in conditional assignment

Started byGeorge Trojan <george.trojan@noaa.gov>
First post2015-11-24 03:25 +0000
Last post2015-11-24 22:25 +1100
Articles 2 — 2 participants

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

  tuples in conditional assignment George Trojan <george.trojan@noaa.gov> - 2015-11-24 03:25 +0000
    Re: tuples in conditional assignment Steven D'Aprano <steve@pearwood.info> - 2015-11-24 22:25 +1100

#99292 — tuples in conditional assignment

FromGeorge Trojan <george.trojan@noaa.gov>
Date2015-11-24 03:25 +0000
Subjecttuples in conditional assignment
Message-ID<mailman.83.1448335555.2291.python-list@python.org>
The following code has bitten me recently:

 >>> t=(0,1)
 >>> x,y=t if t else 8, 9
 >>> print(x, y)
(0, 1) 9

I was assuming that a comma has the highest order of evaluation, that is 
the expression 8, 9 should make a tuple. Why this is not the case?

George

[toc] | [next] | [standalone]


#99315

FromSteven D'Aprano <steve@pearwood.info>
Date2015-11-24 22:25 +1100
Message-ID<56544946$0$1593$c3e8da3$5496439d@news.astraweb.com>
In reply to#99292
On Tue, 24 Nov 2015 02:25 pm, George Trojan wrote:

> The following code has bitten me recently:
> 
>  >>> t=(0,1)
>  >>> x,y=t if t else 8, 9
>  >>> print(x, y)
> (0, 1) 9
> 
> I was assuming that a comma has the highest order of evaluation, that is
> the expression 8, 9 should make a tuple. Why this is not the case?

I'm not sure what sort of answer you are looking for. Why does anything have
the precedence it has?

Making assumptions about the comma's precedence in that way seems unsafe to
me. Consider that function(a, b, c, d) is a function call with four
arguments, NOT a single 4-tuple argument. And:

py> 1, 2, 3 * 2  # expecting (1, 2, 3, 1, 2, 3)
(1, 2, 6)

So there's plenty of evidence that the comma has a very low order of
evaluation, not the highest, and it seems remarkably unsafe to assume the
opposite. Fortunately, it is very simple to test these things out at the
interactive interpreter. I cannot imagine doing any Python programming
without having a Python shell open and ready for me to try out code
snippets.



-- 
Steven

[toc] | [prev] | [standalone]


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


csiph-web