From: Chris Torek Newsgroups: comp.lang.python Subject: Re: Interpreting Left to right? Date: 25 Jun 2011 18:44:09 GMT Organization: None of the Above Lines: 99 Message-ID: References: <20110624200618.GK6075@point.cs.wisc.edu> NNTP-Posting-Host: pd4e486f232bc3db6662541ea79144e4d0316d8893ff6e408.newsdawg.com X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Originator: torek@elf.torek.net (Chris Torek) Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!news.ecp.fr!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!spln!extra.newsguy.com!newsp.newsguy.com!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8450 (Re: x = x['huh'] = {} which binds x to a new dictionary, then binds that dictionary's 'huh' key to the same dictionary...) In article Tycho Andersen 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: and the evaluation order is, in effect and using pseudo-Python: 1. -- the (single) RHS tmp = eval() 2. for in : # left-to-right = tmp When there is only one item in the (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 , 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