Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25963
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: What's wrong with this code? |
| Date | Tue, 24 Jul 2012 09:47:38 +0200 |
| Lines | 58 |
| Message-ID | <sh02e9-iqk.ln1@satorlaser.homedns.org> (permalink) |
| References | <mailman.2478.1343055006.4697.python-list@python.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de Wd8UoBzfUTsGXo7IT1b9/QFbVKwrzZw8nhvA+c49/ZvY07IU1tdFxv9clWQg== |
| X-Orig-Path | satorlaser.homedns.org!not-for-mail |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 |
| In-Reply-To | <mailman.2478.1343055006.4697.python-list@python.org> |
| Xref | csiph.com comp.lang.python:25963 |
Show key headers only | View raw
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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