Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assign': 0.07; 'subject:Question': 0.07; '22,': 0.09; 'explanation': 0.09; 'indexes': 0.09; 'item.': 0.09; 'cc:addr:python-list': 0.11; 'changes': 0.15; '(second': 0.16; 'means.': 0.16; 'mutable': 0.16; 'slice,': 0.16; 'subject:object': 0.16; 'elements': 0.16; 'index': 0.16; 'wrote:': 0.18; '(but': 0.19; 'replacing': 0.19; 'cc:addr:python.org': 0.22; 'example.': 0.24; 'refers': 0.24; 'replace': 0.24; 'cc:2**0': 0.24; 'first,': 0.26; 'defined': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'leave': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'ones.': 0.31; '(including': 0.33; 'url:python': 0.33; 'table': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; '(e.g.,': 0.36; 'sequence': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'so,': 0.37; 'list': 0.37; 'list.': 0.37; 'starting': 0.37; 'url:library': 0.38; 'list,': 0.38; 'pm,': 0.38; 'explain': 0.39; 'new': 0.61; 'url:3': 0.61; 'entire': 0.61; "you're": 0.61; 'back': 0.62; 'name': 0.63; 'taking': 0.65; 'to:addr:gmail.com': 0.65; 'jul': 0.74; 'off,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=tENy2FdWCLx2S2egWFtOitSylkAgme3h+I+RWkWk66k=; b=A3Okh64lNRGQx9jAIosQ2Iu7hlWzapngTErvASFWsO/nxVHBsgJOToolqdCmrn2ci6 PafL/UvCtMJw5iOdn2Qt13MSpJSkzXGNpQwMc0IRHPkIXPCXK2MBUkTFbZFMvnQvB2Vz tKkIUhShVb0SujlM7bXS1z2Kutf2Ld7oAV1BHinbACZ8FTrW9zRC3vMnmFjKgOyoycBF 8BAJh39VR0nUo24P/o9sytOMy/Pa5El9cheqtvm0hWiI1GUkyvCkwmeuURnKhrbg9Vz9 d90+57DJl/rV5NVtl2316b9Yz6B5U/TfjbjQWCgTudeXmKDik3qquuRQYCGdCo1rl047 hZFA== MIME-Version: 1.0 X-Received: by 10.66.66.229 with SMTP id i5mr10527212pat.47.1406070368880; Tue, 22 Jul 2014 16:06:08 -0700 (PDT) In-Reply-To: <6d919cfd-5d3e-4081-94a7-80ce64eedd83@googlegroups.com> References: <986eee35-0327-46e5-bce0-b1ae4572dd8f@googlegroups.com> <6d919cfd-5d3e-4081-94a7-80ce64eedd83@googlegroups.com> Date: Tue, 22 Jul 2014 19:06:08 -0400 Subject: Re: Question about Pass-by-object-reference? From: Jerry Hill To: fl Content-Type: text/plain; charset=UTF-8 Cc: "python-list \(General\)" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406070378 news.xs4all.nl 2837 [2001:888:2000:d::a6]:48653 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75040 On Tue, Jul 22, 2014 at 6:17 PM, fl wrote: > Thanks for your example. I do not find the explanation of [:] on line. Could you > explain it to me, or where can I find it on line? It's pretty hard to find if you don't already know what's going on. First, you need to know that mylst[i:j] refers to a slice of the list "mylist". Specifically, the items from the list starting with the item at index i, up to but not including the item at index j. If you leave the i off (e.g., mylist[:j]) that's a slice from the start of the list up to (but not including) the j-th item. If you leave the end position off, (e.g., mylist[i:]), that gets you the i-th item to the end (including the last item). If you leave off both indexes from the slice, you get back the entire contents of the list. So that's what mylist[:] means. Then you need to know that you can assign to the slice and it will replace the old elements from the slice with the new ones. You can see that defined here, in the docs (second item in the table under "4.6.3. Mutable Sequence Types"): https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types So, when you so mylist[:] = [0,1] you're taking all of the contents of the existing list, and replacing them with the contents of the list [0,1]. That changes the existing list, it doesn't just assign a new list to the name mylist. -- Jerry