Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63669
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'argument': 0.05; '(so': 0.07; 'referring': 0.07; '[1,': 0.09; 'explanation': 0.09; 'here?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; '#python': 0.16; '2.7.3': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'starts': 0.20; '>>>': 0.22; 'memory': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'second': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'forgot': 0.30; 'list:': 0.30; "i'm": 0.30; 'that.': 0.31; '>>>>': 0.31; 'object.': 0.31; 'sep': 0.31; 'thanks!': 0.32; 'could': 0.34; 'there': 0.35; 'list.': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'increased': 0.61; 'new': 0.61; 'simply': 0.61; 'first': 0.61; 'name': 0.63; 'refer': 0.63; 'more': 0.64; 'below.': 0.71; 'idiom': 0.84; '2013,': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Ned Batchelder <ned@nedbatchelder.com> |
| Subject | Re: L[:] |
| Date | Fri, 10 Jan 2014 17:03:49 -0500 |
| References | <1389375507.21198.YahooMailBasic@web163801.mail.gq1.yahoo.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 8bit |
| X-Gmane-NNTP-Posting-Host | 18.189.30.229 |
| User-Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 |
| In-Reply-To | <1389375507.21198.YahooMailBasic@web163801.mail.gq1.yahoo.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5308.1389391443.18130.python-list@python.org> (permalink) |
| Lines | 45 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1389391443 news.xs4all.nl 2953 [2001:888:2000:d::a6]:36276 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:63669 |
Show key headers only | View raw
On 1/10/14 12:38 PM, Albert-Jan Roskam wrote: > In Python Cookbook, one of the authors (I forgot who) consistently used the "L[:]" idiom like below. If the second line simply starts with "L =" (so no "[:]") only the name "L" would be rebound, not the underlying object. That was the authorÅ› explanation as far as I can remember. I do not get that. Why is the "L[:]" idiom more memory-efficient here? How could the increased efficiency be demonstrated? > > #Python 2.7.3 (default, Sep 26 2013, 16:38:10) [GCC 4.7.2] on linux2 >>>> L = [x ** 2 for x in range(10)] >>>> L[:] = ["foo_" + str(x) for x in L] > I'm not sure there is a memory efficiency argument to make here. The big difference is that the first line make L refer to a completely new list, while the second line replaces the contents of an existing list. This makes a big difference if there are other names referring to the list: >>> L = [1, 2, 3, 4] >>> L2 = L >>> L[:] = [] >>> print L2 [] >>> L = [1, 2, 3, 4] >>> L2 = L >>> L = [] >>> print L2 [1, 2, 3, 4] Names and values in Python can be confusing. Here's an explanation of the mechanics: http://nedbatchelder.com/text/names.html HTH, --Ned. > > Thanks! > > > Regards, > > Albert-Jan -- Ned Batchelder, http://nedbatchelder.com
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: L[:] Ned Batchelder <ned@nedbatchelder.com> - 2014-01-10 17:03 -0500
csiph-web