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


Groups > comp.lang.python > #100286

Re: Python variable assigning problems...

From Jussi Piitulainen <harvesting@is.invalid>
Newsgroups comp.lang.python
Subject Re: Python variable assigning problems...
Date 2015-12-11 20:27 +0200
Organization A noiseless patient Spider
Message-ID <lf5oadwhmd5.fsf@ling.helsinki.fi> (permalink)
References (1 earlier) <n4c0of$hqr$1@dont-email.me> <0098ce4a-966b-41df-bf06-352ad451134f@googlegroups.com> <n4etbt$mve$1@news.albasani.net> <mailman.144.1449852005.12405.python-list@python.org> <b09aa197-1945-4df7-8de6-2f2aeb28aba6@googlegroups.com>

Show all headers | View raw


ICT Ezy writes:
> On Friday, December 11, 2015 at 8:40:18 AM UTC-8, Ian wrote:
>> 
>> No, it actually happens left to right. "x = y = z = 0" means "assign
>> 0 to x, then assign 0 to y, then assign 0 to z." It doesn't mean
>> "assign 0 to z, then assign z to y, etc." This works:
>> 
>> >>> d = d['foo'] = {}
>> >>> d
>> {'foo': {...}}
>> 
>> This doesn't:
>> 
>> >>> del d
>> >>> d['foo'] = d = {}
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> NameError: name 'd' is not defined
>
> Deat Ian,
> Thank you very much your answer, but
> above answer from Robin Koch and your answer is different. What's the
> actually process here? I agree with Robin Koch, but your answer is
> correct. Pl explain differences ?

Python language reference, at 7.2 Assignment statements, says this:

# 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.

To simplify a bit, it's talking about a statement of this form:

     target_list = target_list = target_list = expression_list

And it says what Ian said: the value of the expression is assigned to
each target *from left to right*.

<https://docs.python.org/3/reference/simple_stmts.html#assignment-statements>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python variable assigning problems... ICT Ezy <ictezy@gmail.com> - 2015-12-09 09:49 -0800
  Re: Python variable assigning problems... Joel Goldstick <joel.goldstick@gmail.com> - 2015-12-09 12:51 -0500
  Re: Python variable assigning problems... Joel Goldstick <joel.goldstick@gmail.com> - 2015-12-09 12:52 -0500
  Re: Python variable assigning problems... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-09 21:38 +0000
  Re: Python variable assigning problems... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-12-10 16:33 +1100
  Re: Python variable assigning problems... Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-10 14:03 +0000
    Re: Python variable assigning problems... ICT Ezy <ictezy@gmail.com> - 2015-12-11 08:10 -0800
      Re: Python variable assigning problems... Robin Koch <robin.koch@t-online.de> - 2015-12-11 17:24 +0100
        Re: Python variable assigning problems... Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-11 09:39 -0700
          Re: Python variable assigning problems... Robin Koch <robin.koch@t-online.de> - 2015-12-11 18:52 +0100
            Re: Python variable assigning problems... ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:23 -0800
          Re: Python variable assigning problems... ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:05 -0800
            Re: Python variable assigning problems... Michael Torrie <torriem@gmail.com> - 2015-12-11 11:20 -0700
              Re: Python variable assigning problems... ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:25 -0800
            Re: Python variable assigning problems... Jussi Piitulainen <harvesting@is.invalid> - 2015-12-11 20:27 +0200
              Re: Python variable assigning problems... ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:54 -0800
              Re: Python variable assigning problems... ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:55 -0800
        Re: Python variable assigning problems... ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:00 -0800
          Re: Python variable assigning problems... Michael Torrie <torriem@gmail.com> - 2015-12-11 11:10 -0700
      Re: Python variable assigning problems... Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-11 09:36 -0700

csiph-web