Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: What's wrong with this code?

Reply
Thread Tools

Re: What's wrong with this code?

 
 
Chris Angelico
Guest
Posts: n/a
 
      07-23-2012
On Tue, Jul 24, 2012 at 12:50 AM, Stone Li <(E-Mail Removed)> wrote:
>
> I'm totally confused by this code:
>
> Code:


Boiling it down to just the bit that matters:

c = None
d = None
x = [c,d]
e,f = x
c = 1
d = 2
print e,f

When you assign "e,f = x", you're taking the iterable x and unpacking
its contents. There's no magical "referenceness" that makes e bind to
the same thing as c; all that happens is that the objects in x gain
additional references. When you rebind c and d later, that doesn't
change x, nor e/f.

What you've done is just this:

x = [None, None]
e,f = x
c = 1
d = 2
print e,f

It's clear from this version that changing c and d shouldn't have any
effect on e and f. In Python, any time you use a named variable in an
expression, you can substitute the object that that name is
referencing - it's exactly the same. (That's one of the things I love
about Python. No silly rules about what you can do with a function
return value - if you have a function that returns a list, you can
directly subscript or slice it. Yay!)

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Have I bought wrong product? enquirer Wireless Networking 2 06-10-2005 10:59 PM
Zero Config keeps connecting to the wrong AP =?Utf-8?B?ZGdyaWZmaXRo?= Wireless Networking 2 03-04-2005 05:52 PM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
wrong connection status Peter Welk Wireless Networking 0 12-22-2004 03:26 PM
XP SP2 Wrong IP on connection D Wells Wireless Networking 3 12-09-2004 03:35 AM



Advertisments