What's wrong with this code?

S

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?
And this question made my GUI program totally out of control.
Thanks[?]
 
A

Andrew Cooper

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
 
M

Mark Lawrence

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>
 
D

Devin Jeanpierre

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
 
U

Ulrich Eckhardt

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:

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


"[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".


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


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

Chris Angelico

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
 
I

Ian Kelly

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

Steven D'Aprano

[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.
 
T

Thomas Rachel

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
 
T

Thomas Rachel

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
 
U

Ulrich Eckhardt

Am 24.07.2012 10:24, schrieb Chris Angelico:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top