[BangPypers] List of n but same objects

Rajiv Subramanian M rajiv.m1991 at gmail.com
Thu Dec 11 11:51:00 CET 2014


Hi Shreyas,

Thanks for your answer.
For the questions numered 2 and 3 do you have any thoughts?

2. Is there any other possibility or way (like * operator does here) by
which we can obtain the same result as in CASE 1?
3. Does only list and listiterators objects can behave this way? what other
python datatypes can behave this way?



On Thu, Dec 11, 2014 at 4:14 PM, Kulkarni, Shreyas <shyran at gmail.com> wrote:

> When you call iter(x) it returns you a listiterator object. Every time you
> call iter(x) it *creates* a new listiterator and returns it back. The
> differences you are seeing in your cases are not because of how lists of
> list operators work, but because of how they are called.
>
> In case-1, iter(x) gets called once, and the same returned object is used
> when you do '* 3' on the list.
> In case-2, you are calling iter(x) three times, so naturally you get 3
> different iterators - three different objects with different base addresses
> in memory.
>
> Case-3 is how python typically works - when you do an assignment, python
> doesn't create a copy, but a reference. So when you say y = [x] * 3; y is
> essentially a list with 3 references to the same memory location pointed to
> by x. So when you update one value, all three refs point to the same
> location, and hence you are seeing what you are seeing.
> If you want deep copy instead of shallow one with references, take a look
> at 'copy' module and copy.deepcopy method in particular.
>
> shreyas
>
>
> On Dec 11, 2014, at 3:58 PM, Rajiv Subramanian M <rajiv.m1991 at gmail.com>
> wrote:
>
> > Hello Group,
> >
> > I am Rajiv, Python/Django developer in a startup, Bangalore. Today when I
> > experimenting with python I came across the following
> >
> > CASE 1:
> >
> >>>> x = range(10)
> >
> >>>> [iter(x)] * 3
> >
> > [<listiterator object at 0x7f6aa5594850>,
> >
> > <listiterator object at 0x7f6aa5594850>,
> >
> > <listiterator object at 0x7f6aa5594850>]
> >
> >
> > Thing to Note:
> >
> > Here all the 3 listiterator object in the list is actually a same single
> > instance residing at the memory location "0x7f6aa5594850"
> >
> >
> > CASE 2:
> >
> >>>> [iter(x), iter(x), iter(x)]
> >
> > [<listiterator object at 0x7f6aa5594890>,
> >
> > <listiterator object at 0x7f6aa55948d0>,
> >
> > <listiterator object at 0x7f6aa5594910>]
> >
> >
> > Thing to Note:
> > In this case literally I created called the iter(x) for 3 times so it
> > created 3 different listiterator object.
> >
> > CASE 3:
> >>>> x = [1, 2, 3]
> >>>> y = [x] * 3
> >>>> y
> > [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
> >>>> y[0][0] = 5
> >>>> y
> > [[5, 2, 3], [5, 2, 3], [5, 2, 3]]
> >>>> x
> > [5, 2, 3]
> >
> > Things to Note:
> > As like in first case, here the list objects inside the list y are not
> the
> > duplicates of x but the x itself
> >
> >
> > Question:
> > 1. How in the first case i.e   [iter(x)] * 3  creates a list of 3 but the
> > same objects?
> > 2. Is there any other possibility or way (like * operator does here) by
> > which we can obtain the same result as in CASE 1?
> > 3. Does only list and listiterators objects can behave this way? what
> other
> > python datatypes can behave this way?
> >
> > ~ Regards
> > Rajiv M
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > https://mail.python.org/mailman/listinfo/bangpypers
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>



-- 

  [image: --]
Rajiv Subramanian M
[image: http://]about.me/rajiv.m1991
     <http://about.me/rajiv.m1991?promo=email_sig>


More information about the BangPypers mailing list