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


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

Re: Interpreting Left to right?

Started byTerry Reedy <tjreedy@udel.edu>
First post2011-06-24 02:20 -0400
Last post2011-06-24 02:20 -0400
Articles 1 — 1 participant

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

  Re: Interpreting Left to right? Terry Reedy <tjreedy@udel.edu> - 2011-06-24 02:20 -0400

#8349 — Re: Interpreting Left to right?

FromTerry Reedy <tjreedy@udel.edu>
Date2011-06-24 02:20 -0400
SubjectRe: Interpreting Left to right?
Message-ID<mailman.352.1308896423.1164.python-list@python.org>
On 6/24/2011 12:32 AM, Chetan Harjani wrote:
> x=y="some string"
> And we know that python interprets from left to right.

Read the doc. "5.14. Evaluation order
Python evaluates expressions from left to right. Notice that while 
evaluating an assignment, the right-hand side is evaluated before the 
left-hand side."

> another example:
> (1,2) + 3,
> here, python raises a  TypeError "can only concatenate tuple(not int) to
> tuple" but we know (3,) is a tuple as seen by following:

But "(3,) is not what you wrote;-). The comma operator has the lowest 
precedence, although this is not as clear in the doc as it should be. 
Your expression is parsed as ((1,2)+3),. Parentheses have the highest 
precedence. The combination of both facts is why tuples often need to be 
parenthesized, as it should be here and why you added the first pair 
instead of writing 1,2 + 3,.

Disassembly of bytecode shows how an expression was parsed.
 >>> from dis import dis
 >>> dis('(1,2)+3,')
   1           0 LOAD_CONST               3 ((1, 2))
               3 LOAD_CONST               2 (3)
               6 BINARY_ADD
               7 BUILD_TUPLE              1
              10 RETURN_VALUE

-- 
Terry Jan Reedy

[toc] | [standalone]


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


csiph-web