Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99293
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ben Finney <ben+python@benfinney.id.au> |
| Newsgroups | comp.lang.python |
| Subject | Re: tuples in conditional assignment |
| Date | Tue, 24 Nov 2015 15:49:04 +1100 |
| Lines | 53 |
| Message-ID | <mailman.84.1448340562.2291.python-list@python.org> (permalink) |
| References | <mailman.36.1447434002.19880.python-list@python.org> <5653D8BC.1040800@noaa.gov> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| X-Trace | news.uni-berlin.de lzARkGTA0v70kzXg9d3uYg5yrLzWQmHM+8Sitv5MXSqg== |
| Cancel-Lock | sha1:7EGMaXunE+knWAku2l9I8/DhmY4= |
| 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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; 'that?': 0.05; 'assignment': 0.07; 'expressions': 0.07; '(0,': 0.09; 'assigning': 0.09; 'assumed': 0.09; 'operator,': 0.09; 'precedence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rules.': 0.09; 'simplified': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'assume': 0.11; ':-)': 0.12; '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:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'tells': 0.18; '>>>': 0.20; '(not': 0.20; 'assuming': 0.22; 'satisfying': 0.22; 'trying': 0.22; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'least': 0.27; 'yields': 0.29; "i'm": 0.30; 'code': 0.30; 'mention': 0.30; 'rules': 0.31; 'are:': 0.32; 'url:python': 0.33; 'list': 0.34; 'on,': 0.35; 'should': 0.36; 'url:org': 0.36; 'evaluation': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'list.': 0.37; 'names': 0.38; 'anything': 0.38; 'why': 0.39; 'sure': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'url:3': 0.60; 'complete': 0.63; '8bit%:21': 0.70; 'skip:\xe2 10': 0.70; '_o__)': 0.84; 'bitten': 0.84; 'case?': 0.84; 'descriptive': 0.84; 'received:125': 0.84; 'recently:': 0.84; 'universe': 0.84; 'trojan': 0.91; 'url:reference': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | jigong.madmonks.org |
| X-Public-Key-ID | 0xAC128405 |
| X-Public-Key-Fingerprint | 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 |
| X-Public-Key-URL | http://www.benfinney.id.au/contact/bfinney-pubkey.asc |
| X-Post-From | Ben Finney <bignose+hates-spam@benfinney.id.au> |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) |
| 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> |
| Xref | csiph.com comp.lang.python:99293 |
Show key headers only | View raw
George Trojan <george.trojan@noaa.gov> 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.
<URL:https://docs.python.org/3/reference/expressions.html#expression-lists>
> 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?
<URL:https://docs.python.org/3/reference/expressions.html#operator-precedence>
> 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’).
--
\ “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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: tuples in conditional assignment Ben Finney <ben+python@benfinney.id.au> - 2015-11-24 15:49 +1100
csiph-web