[BangPypers] List of n but same objects

Abdul Muneer abdulmuneer at gmail.com
Thu Dec 11 11:54:14 CET 2014


Hi Rajiv,
Premise 1:
Every time you *create* an object, it has a new id. Examples:
>>> a = list ()
>>> b = list()
>>> id(a), id(b)
(140688241027768, 140688241027840)

>>> p = dict()
>>> q = dict()
>>> id(p), id(q)
(140688241071280, 140688241071560)

>>> x = iter(range(10))
>>> y = iter(range(10))
>>> id(x), id(y)
(140688241024208, 140688241024272)


Premise 2:
Every time you associate the same object to multiple names, they all will
point to same object. Examples:
>>> a = list()
>>> b = a
>>> id(a), id(b)
(140688241028200, 140688241028200)
>>> p = dict()
>>> q = p
>>> id(p), id(q)
(140688241071840, 140688241071840)
>>> x = iter(range(10))
>>> y = x
>>> id(x), id(y)
(140688241024464, 140688241024464)


In your problem, Case 1 and Case 3 follow the pattern [an_object]*3 which
results in [an_object, an_object, an_object] where as Case 2 follows the
pattern [create_object(), create_object(), create_object()]
Note that in Case 3, iter(x) is evaluated before multiplying.

Regards,
Abdul Muneer

--
Follow me on Twitter: @abdulmuneer <http://twitter.com/#%21/abdulmuneer>

On Thu, Dec 11, 2014 at 4:21 PM, Rajiv Subramanian M <rajiv.m1991 at gmail.com>
wrote:
>
> 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>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list