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


Groups > comp.lang.python > #25883 > unrolled thread

What's wrong with this code?

Started byStone Li <viewfromoffice@gmail.com>
First post2012-07-23 22:50 +0800
Last post2012-07-24 10:34 +0200
Articles 11 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  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

#25883 — What's wrong with this code?

FromStone Li <viewfromoffice@gmail.com>
Date2012-07-23 22:50 +0800
SubjectWhat's wrong with this code?
Message-ID<mailman.2478.1343055006.4697.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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[?]

[toc] | [next] | [standalone]


#25943

FromAndrew Cooper <amc96@cam.ac.uk>
Date2012-07-24 02:19 +0100
Message-ID<BKmPr.135207$Hs3.55909@fx08.am4>
In reply to#25883
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

[toc] | [prev] | [next] | [standalone]


#25956

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-07-24 07:23 +0100
Message-ID<mailman.2518.1343110960.4697.python-list@python.org>
In reply to#25943
On 24/07/2012 02:19, Andrew Cooper wrote:
> 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
>

<duck and cover>

strictly speaking Python doesn't have variables, it has names.  This 
will possibly start a flame war which, by the standards of this ng/ml, 
will be an intense conflagration, hence the duck and cover.

</duck and cover>

-- 
Cheers.

Mark Lawrence.

[toc] | [prev] | [next] | [standalone]


#25959

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2012-07-24 03:38 -0400
Message-ID<mailman.2519.1343115577.4697.python-list@python.org>
In reply to#25943
On Tue, Jul 24, 2012 at 2:23 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> strictly speaking Python doesn't have variables, it has names.  This will
> possibly start a flame war which, by the standards of this ng/ml, will be an
> intense conflagration, hence the duck and cover.

The two terms are nearly synonymous when one talks about Python (and
both are used in the language reference).

I mean, what's so "strict" about the way you're speaking?

-- Devin

[toc] | [prev] | [next] | [standalone]


#25970

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-07-24 02:26 -0600
Message-ID<mailman.2524.1343120256.4697.python-list@python.org>
In reply to#25943
On Mon, Jul 23, 2012 at 7:19 PM, Andrew Cooper <amc96@cam.ac.uk> wrote:
> Python is a statically scoped language, whereas the functionality you
> are expecting would be an example of dynamically scoped.

While it's true that Python is statically scoped, that has nothing at
all to do with the code from the OP's question, which contains only
one (global) scope anyway.

The real issue is confusion between name binding and object mutation.
By reassigning c and d, the OP is evidently trying to mutate the list
named x (or to be more precise, the list that is the second element of
the list named x).  But this doesn't work, because name-binding
doesn't mutate objects as it does in languages with variable semantics
(C, for example); it merely rebinds the names to different objects.  c
and d end up bound to values that weren't in the list to begin with,
meanwhile the list remains unchanged and e and f are still just bound
to None.

[toc] | [prev] | [next] | [standalone]


#25963

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-07-24 09:47 +0200
Message-ID<sh02e9-iqk.ln1@satorlaser.homedns.org>
In reply to#25883
There is one model that has helped me much understanding how Python 
ticks and that is the model of name tags. The code "a = 1" creates an 
integer with value 1 and attaches a tag with "a" written on it using a 
small piece of rope. Now, if you attach the tag to a different item, it 
obviously doesn't change the original integer. Also, you can attach more 
than one tag to the integer or even none. Further, a tag doesn't even 
have to be attached to anything (this happens if you use a local 
variable before assigning to it). This operation of tagging something is 
done with the "=" operator.

Now, coming back to your code...

Am 23.07.2012 16:50, schrieb Stone Li:
> I'm totally confused by this code:
>
> Code:
>
>> a = None
>> b = None
>> c = None
>> d = None

This adds the tags "a", "b", "c" and "d" to None.


>> x = [[a,b],
>>       [c,d]]

"[a, b]" creates a list, containing two anonymous tags (they don't have 
anything written on them but they are accessible via index) attached to 
what "a" and "b" are currently attached to [0]. The same happens for 
"[c, d]". The two lists are then put into another list with a similar 
mechanism, and that list of lists is then tagged "x".


>> e,f = x[1]

This takes the second element of "x" (the [c, d] above) and tags it with 
"e" and "f". This syntax implicitly unpacks the list so the assignment 
operator adds the two tags "e" and "f" to the first and second element 
referenced by that list. Both "e" and "f" finally end up attached to "None".


>> c = 1
>> d = 2

These two remove the rope attaching "c" and "d" to "None" and instead 
attach them to the integers "1" and "2".


I hope your Python's behaviour makes sense to you now!

Uli


[0] Note that in almost all cases, when referring to a tag, Python 
implicitly operates on the object attached to it. One case (the only 
one?) where it doesn't is the "del" statement.

[toc] | [prev] | [next] | [standalone]


#25965

FromChris Angelico <rosuav@gmail.com>
Date2012-07-24 18:24 +1000
Message-ID<mailman.2523.1343118300.4697.python-list@python.org>
In reply to#25963
On Tue, Jul 24, 2012 at 5:47 PM, Ulrich Eckhardt
<ulrich.eckhardt@dominolaser.com> wrote:
> There is one model that has helped me much understanding how Python ticks
> and that is the model of name tags. The code "a = 1" creates an integer with
> value 1 and attaches a tag with "a" written on it using a small piece of
> rope.

A double strand of rope, I think. If it were one strand, we'd write "a - 1". :)

> [0] Note that in almost all cases, when referring to a tag, Python
> implicitly operates on the object attached to it. One case (the only one?)
> where it doesn't is the "del" statement.

I'd say that del is more closely related to assignment than anything
else. You can go "a = None" to mark that a now points to nothing, or
you can "del a" to de-rope a altogether.

ChrisA

[toc] | [prev] | [next] | [standalone]


#25975

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-07-24 12:05 +0200
Message-ID<dk82e9-idl.ln1@satorlaser.homedns.org>
In reply to#25965
Am 24.07.2012 10:24, schrieb Chris Angelico:
> On Tue, Jul 24, 2012 at 5:47 PM, Ulrich Eckhardt
> <ulrich.eckhardt@dominolaser.com> wrote:
>> There is one model that has helped me much understanding how Python ticks
>> and that is the model of name tags. The code "a = 1" creates an integer with
>> value 1 and attaches a tag with "a" written on it using a small piece of
>> rope.
>
> A double strand of rope, I think. If it were one strand, we'd write "a - 1". :)

Two fibers, possibly twisted.... let's call it string!

I really had to think about how to call the thing while avoiding the 
term string in order not to add to the confusion...

(:

Uli

[toc] | [prev] | [next] | [standalone]


#25966

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-07-24 08:26 +0000
Message-ID<500e5c4c$0$1779$c3e8da3$76491128@news.astraweb.com>
In reply to#25963
On Tue, 24 Jul 2012 09:47:38 +0200, Ulrich Eckhardt wrote:

> [0] Note that in almost all cases, when referring to a tag, Python
> implicitly operates on the object attached to it. One case (the only
> one?) where it doesn't is the "del" statement.

Name-binding:

x = 1

operates on the name "x", not the object. The object 1 does not know it 
has been bound to anything -- that's one weakness of the "tag" model, 
because it implies that objects know what tags they have attached. They 
don't.

Imports:

import x

also operates on the name x and not the object.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#25971

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2012-07-24 11:34 +0200
Message-ID<julq7i$v46$1@r03.glglgl.gl>
In reply to#25963
Am 24.07.2012 09:47 schrieb Ulrich Eckhardt:

> [0] Note that in almost all cases, when referring to a tag, Python
> implicitly operates on the object attached to it. One case (the only
> one?) where it doesn't is the "del" statement.

The del and the =, concerning the left side.

But even those don't do that under all circumstances. Think about 
__setitem__, __setattr__, __set__, __delitem__, __delattr__, __delete__.


Thomas


[toc] | [prev] | [next] | [standalone]


#25968

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2012-07-24 10:34 +0200
Message-ID<julmmp$s13$1@r03.glglgl.gl>
In reply to#25883
Am 23.07.2012 16:50 schrieb Stone Li:
> 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?

Your expectation :-)

With c = 1 and d = 2 you do not change the respective objects, but you 
assign other objects to the same names.

The old content is still contained in x[1].

If you would only modify these objects (not possible as ints are 
immutable), you would notice the changes here and there.


Thomas

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web