Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75040

Re: Question about Pass-by-object-reference?

References <cc0616eb-642a-426c-a995-6665ee4c52b9@googlegroups.com> <986eee35-0327-46e5-bce0-b1ae4572dd8f@googlegroups.com> <c3808lFn5iuU1@mid.individual.net> <mailman.12193.1406062021.18130.python-list@python.org> <6d919cfd-5d3e-4081-94a7-80ce64eedd83@googlegroups.com>
Date 2014-07-22 19:06 -0400
Subject Re: Question about Pass-by-object-reference?
From Jerry Hill <malaclypse2@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.12206.1406070378.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Jul 22, 2014 at 6:17 PM, fl <rxjwg98@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 12:04 -0700
  Re: Question about Pass-by-object-reference? Ned Batchelder <ned@nedbatchelder.com> - 2014-07-22 15:32 -0400
    Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 12:54 -0700
  Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 12:34 -0700
    Re: Question about Pass-by-object-reference? Peter Pearson <ppearson@nowhere.invalid> - 2014-07-22 20:35 +0000
      Re: Question about Pass-by-object-reference? emile <emile@fenx.com> - 2014-07-22 13:46 -0700
        Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 15:17 -0700
          Re: Question about Pass-by-object-reference? Joel Goldstick <joel.goldstick@gmail.com> - 2014-07-22 18:26 -0400
          Re: Question about Pass-by-object-reference? emile <emile@fenx.com> - 2014-07-22 15:33 -0700
          Re: Question about Pass-by-object-reference? Jerry Hill <malaclypse2@gmail.com> - 2014-07-22 19:06 -0400
        Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 15:31 -0700
          Re: Question about Pass-by-object-reference? emile <emile@fenx.com> - 2014-07-22 15:40 -0700
      Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 16:00 -0700
        Re: Question about Pass-by-object-reference? emile <emile@fenx.com> - 2014-07-22 16:10 -0700
  Re: Question about Pass-by-object-reference? Terry Reedy <tjreedy@udel.edu> - 2014-07-22 20:27 -0400
    Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 18:49 -0700
      Re: Question about Pass-by-object-reference? Ben Finney <ben+python@benfinney.id.au> - 2014-07-23 11:59 +1000
        Re: Question about Pass-by-object-reference? Steven D'Aprano <steve@pearwood.info> - 2014-07-23 05:35 +0000
          Re: Question about Pass-by-object-reference? Chris Angelico <rosuav@gmail.com> - 2014-07-23 16:07 +1000
          Re: Question about Pass-by-object-reference? Ben Finney <ben+python@benfinney.id.au> - 2014-07-23 16:25 +1000
          Re: Question about Pass-by-object-reference? Terry Reedy <tjreedy@udel.edu> - 2014-07-23 18:51 -0400
            Re: Question about Pass-by-object-reference? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-24 03:22 +0000
              Re: Question about Pass-by-object-reference? Steven D'Aprano <steve@pearwood.info> - 2014-07-24 05:05 +0000
    Re: Question about Pass-by-object-reference? Steven D'Aprano <steve@pearwood.info> - 2014-07-23 05:36 +0000
      Re: Question about Pass-by-object-reference? Terry Reedy <tjreedy@udel.edu> - 2014-07-23 18:32 -0400
    Re: Question about Pass-by-object-reference? alex23 <wuwei23@gmail.com> - 2014-07-25 15:20 +1000
  Re: Question about Pass-by-object-reference? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-23 01:56 +0000

csiph-web