Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   What's wrong with this code? (http://www.velocityreviews.com/forums/t948445-whats-wrong-with-this-code.html)

Stone Li 07-23-2012 02:50 PM

What's wrong with this code?
 
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[?]


Andrew Cooper 07-24-2012 01:19 AM

Re: What's wrong with this code?
 
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

Mark Lawrence 07-24-2012 06:23 AM

Re: What's wrong with this code?
 
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.


Devin Jeanpierre 07-24-2012 07:38 AM

Re: What's wrong with this code?
 
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

Ulrich Eckhardt 07-24-2012 07:47 AM

Re: What's wrong with this code?
 
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.

Chris Angelico 07-24-2012 08:24 AM

Re: What's wrong with this code?
 
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

Ian Kelly 07-24-2012 08:26 AM

Re: What's wrong with this code?
 
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.

Steven D'Aprano 07-24-2012 08:26 AM

Re: What's wrong with this code?
 
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

Thomas Rachel 07-24-2012 08:34 AM

Re: What's wrong with this 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
> 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

Thomas Rachel 07-24-2012 09:34 AM

Re: What's wrong with this code?
 
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





All times are GMT. The time now is 10:17 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.