(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-l...@python.org> Tycho Andersen <ty...@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
-- http://mail.python.org/mailman/listinfo/python-list