Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8410 > unrolled thread
| Started by | Tycho Andersen <tycho@tycho.ws> |
|---|---|
| First post | 2011-06-24 16:08 -0500 |
| Last post | 2011-06-25 18:44 +0000 |
| Articles | 3 — 3 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.
Re: Interpreting Left to right? Tycho Andersen <tycho@tycho.ws> - 2011-06-24 16:08 -0500
Re: Interpreting Left to right? Ben Finney <ben+python@benfinney.id.au> - 2011-06-25 10:32 +1000
Re: Interpreting Left to right? Chris Torek <nospam@torek.net> - 2011-06-25 18:44 +0000
| From | Tycho Andersen <tycho@tycho.ws> |
|---|---|
| Date | 2011-06-24 16:08 -0500 |
| Subject | Re: Interpreting Left to right? |
| Message-ID | <mailman.389.1308949722.1164.python-list@python.org> |
On Fri, Jun 24, 2011 at 01:24:24PM -0700, Ned Deily wrote:
> In article <20110624200618.GK6075@point.cs.wisc.edu>,
> Tycho Andersen <tycho@tycho.ws> wrote:
> > Yes, I understand that, but I guess I don't understand *why* things
> > are done that way. What is the evaluation order principle at work
> > here? I would have expected:
> >
> > tmp = {}
> > x['huh'] = tmp # NameEror!
> >
> > That is, the right hand sides of assignments are evaluated before the
> > left hand sides. That is (somehow?) not the case here.
>
> http://docs.python.org/py3k/reference/simple_stmts.html#assignment-statements
Perhaps I'm thick, but (the first thing I did was read the docs and) I
still don't get it. From the docs:
"An assignment statement evaluates the expression list (remember that
this can be a single expression or a comma-separated list, the latter
yielding a tuple) and assigns the single resulting object to each of
the target lists, from left to right."
For a single target, it evaluates the RHS and assigns the result to
the LHS. Thus
x = x['foo'] = {}
first evaluates
x['foo'] = {}
which should raise a NameError, since x doesn't exist yet. Where am I
going wrong?
Thanks,
\t
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-06-25 10:32 +1000 |
| Message-ID | <878vsqoimp.fsf@benfinney.id.au> |
| In reply to | #8410 |
Tycho Andersen <tycho@tycho.ws> writes:
> On Fri, Jun 24, 2011 at 01:24:24PM -0700, Ned Deily wrote:
> > http://docs.python.org/py3k/reference/simple_stmts.html#assignment-statements
>
> Perhaps I'm thick, but (the first thing I did was read the docs and) I
> still don't get it. From the docs:
>
> "An assignment statement evaluates the expression list (remember that
> this can be a single expression or a comma-separated list, the latter
> yielding a tuple) and assigns the single resulting object to each of
> the target lists, from left to right."
Notice that, in the grammar given there, there is exactly one
“expression list”, following *all* of the ‘=’s. The “target lists” are
each to the left of an ‘=’.
> For a single target, it evaluates the RHS and assigns the result to
> the LHS. Thus
>
> x = x['foo'] = {}
>
> first evaluates
>
> x['foo'] = {}
No, that's not an “expression list” by the grammar given in the docs.
The expression list consists, in your example, of “{}” only. The target
lists are “x”, then “x['foo']”, in that order.
--
\ “If consumers even know there's a DRM, what it is, and how it |
`\ works, we've already failed.” —Peter Lee, Disney corporation, |
_o__) 2005 |
Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Chris Torek <nospam@torek.net> |
|---|---|
| Date | 2011-06-25 18:44 +0000 |
| Message-ID | <iu5a9p0s3u@news1.newsguy.com> |
| In reply to | #8410 |
(Re:
x = x['huh'] = {}
which binds x to a new dictionary, then binds that dictionary's 'huh'
key to the same dictionary...)
In article <mailman.389.1308949722.1164.python-list@python.org>
Tycho Andersen <tycho@tycho.ws> wrote:
>Perhaps I'm thick, but (the first thing I did was read the docs and) I
>still don't get it. From the docs:
>
>"An assignment statement evaluates the expression list (remember that
>this can be a single expression or a comma-separated list, the latter
>yielding a tuple) and assigns the single resulting object to each of
>the target lists, from left to right."
The "target list" in this case is, in effect:
evail("x"), eval("x['huh']")
>For a single target, it evaluates the RHS and assigns the result to
>the LHS. Thus
>
>x = x['foo'] = {}
>
>first evaluates
>
>x['foo'] = {}
>
>which should raise a NameError, since x doesn't exist yet. Where am I
>going wrong?
I believe you are still reading this as:
x = (something)
and setting aside "x" and "something", and only then peering into the
"something" and finding:
x['foo'] = {}
and -- while keeping all of the other "x = (something)" at bay, trying
to do the x['foo'] assignment.
This is the wrong way to read it!
The correct way to read it is:
- Pseudo_eval("x") and pseudo_eval("x['foo']") are both to be set
to something, so before we look any more closely at the "x" and
"x['foo']" part, we need to evaluate the "something" part.
- The "something" part is: {}, so create a dictionary. There is
no name bound to this result, but for discussion let's bind "tmp"
to it.
- Now that we have evaluated the RHS of the assignment statement
(which we are calling "tmp" even though it has no actual name),
*now* we can go eval() (sort of -- we only "evaluate" them for
assignment, rather than for current value) the pieces of the LHS.
- The first piece of the LHS is "x". Eval-ing x for assignment
gets us the as-yet-unbound "x", and we do:
x = tmp
which binds x to the new dictionary.
- The second piece of the LHS is "x['foo']". Eval-ing this for
assignment gets us the newly-bound x, naming the dictionary;
the key 'foo', a string; and now we bind x['foo'], doing:
x['foo'] = tmp
which makes the dictionary contain itself.
Again, Python's assignment statement (not expression) has the form:
<one or more "LHS =" parts, AKA "target list"> <expression-list>
and the evaluation order is, in effect and using pseudo-Python:
1. <expression-list> -- the (single) RHS
tmp = eval(<expression-list>)
2. for <LHS-part> in <target-list>: # left-to-right
<LHS-part> = tmp
When there is only one item in the <target-list> (i.e., just one
"x =" part in the whole statement), or when all of the parts of
the target-list are independent of each other and of the
<expression-list>, the order does not matter. When the parts are
interdependent, then this left-to-right order *is* important.
--
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web