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


Groups > comp.lang.python > #25943

Re: What's wrong with this code?

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!multikabel.net!newsfeed20.multikabel.net!eweka.nl!lightspeed.eweka.nl!69.16.177.246.MISMATCH!cyclone03.ams2.highwinds-media.com!news.highwinds-media.com!voer-me.highwinds-media.com!npeersf03.am4!fx08.am4.POSTED!not-for-mail
From Andrew Cooper <amc96@cam.ac.uk>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0
MIME-Version 1.0
Newsgroups comp.lang.python
Subject Re: What's wrong with this code?
References <mailman.2478.1343055006.4697.python-list@python.org>
In-Reply-To <mailman.2478.1343055006.4697.python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
Lines 51
Message-ID <BKmPr.135207$Hs3.55909@fx08.am4> (permalink)
NNTP-Posting-Host 86.23.15.89
X-Complaints-To http://netreport.virginmedia.com
X-Trace fx08.am4 1343092769 86.23.15.89 (Tue, 24 Jul 2012 01:19:29 UTC)
NNTP-Posting-Date Tue, 24 Jul 2012 01:19:29 UTC
Organization virginmedia.com
Date Tue, 24 Jul 2012 02:19:29 +0100
X-Received-Bytes 1872
Xref csiph.com comp.lang.python:25943

Show key headers only | View raw


On 23/07/2012 15:50, Stone Li wrote:
> I'm totally confused by this code:
> 
> Code:
> 
>     a = None
>     b = None
>     c = None
>     d = None
>     x = [[a,b],
>          [c,d]]
>     e,f = x[1]
>     print e,f
>     c = 1
>     d = 2
>     print e,f
>     e = 1
>     f = 2
>     print c,d
> 
> Output:
> 
>     None None
>     None None
>     1 2
>      
> 
> 
> I'm expecting the code as:
> 
>     None None
>     1 2
>     1 2
>      
> 
> 
> What's wrong?
> And this question made my GUI program totally out of control.
> Thanks

c = 1 and d = 2 are overwriting the variable c (= None) and d (= None)
with new variables 1 and 2.  As x already captured c and d while they
were none, the variables e and f do not change (not would the, even if
you subsequently changed x)

Python is a statically scoped language, whereas the functionality you
are expecting would be an example of dynamically scoped.

Care to reveal your programming background?

~Andrew

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


Thread

What's wrong with this code? Stone Li <viewfromoffice@gmail.com> - 2012-07-23 22:50 +0800
  Re: What's wrong with this code? Andrew Cooper <amc96@cam.ac.uk> - 2012-07-24 02:19 +0100
    Re: What's wrong with this code? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-07-24 07:23 +0100
    Re: What's wrong with this code? Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-07-24 03:38 -0400
    Re: What's wrong with this code? Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-24 02:26 -0600
  Re: What's wrong with this code? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-07-24 09:47 +0200
    Re: What's wrong with this code? Chris Angelico <rosuav@gmail.com> - 2012-07-24 18:24 +1000
      Re: What's wrong with this code? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-07-24 12:05 +0200
    Re: What's wrong with this code? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-24 08:26 +0000
    Re: What's wrong with this code? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-07-24 11:34 +0200
  Re: What's wrong with this code? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-07-24 10:34 +0200

csiph-web