[BangPypers] List of n but same objects
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@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] List of n but same objects
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.m1...@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@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] List of n but same objects
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 shy...@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.m1...@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@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@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@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] List of n but same objects
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.m1...@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 shy...@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.m1...@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@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@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@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] List of n but same objects
2. I assume you want same result as case 1 - in case 2; case 3 is essentially same as case 1 (which is why you are seeing one assignment 'changing' all three lists). Well, instead of calling iter(x) three times, you need to call it only once, and then use that object to multiply. In short, to get result as case-1, do as case-1 does, instead of case-2 :) 3. I think you are missing the point. This behavior is not tied to list or listiterators or broadly to any datatype in python, but to the way operators, assignments and function calls work in python. shreyas On Dec 11, 2014, at 4:21 PM, Rajiv Subramanian M rajiv.m1...@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 shy...@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.m1...@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@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@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@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] List of n but same objects
On Thu, Dec 11 2014, Rajiv Subramanian M wrote: [...] Question: 1. How in the first case i.e [iter(x)] * 3 creates a list of 3 but the same objects? The * operator when applied to a list (or any sequence type) is a repetition. You can see the code for list_repeat here [1]. The line I've linked to shows what's happening. You allocate enough memory for a new list that's large enough and copy over stuff from the source to the target those many times. Multiplying a list by `n` returns a new list with each element of the said list repeated `n` times. 2. Is there any other possibility or way (like * operator does here) by which we can obtain the same result as in CASE 1? You could do something like this. x = range(10) [iter(x) for x in range(3)] This will call 3 iter times and give you 3 separate iterators. I think though that you're doing something wrong here. What's the larger problem you're trying to solve? 3. Does only list and listiterators objects can behave this way? what other python datatypes can behave this way? This the semantics of sequence types. I haven't checked for tuples etc. but the list sequence methods behave this way. I don't think the others will be different. It doesn't make sense to make copies of the original thing anyway. You can do that explicitly using copy or something if you want to. [...] Footnotes: [1] https://hg.python.org/cpython/file/ce66b65ad8d6/Objects/listobject.c#l567 -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
[BangPypers] [Jobs] Openings for full-time Interns, entry-, mid- and expert-level Python programmers.
Riptide IO http://riptideio.com/ has multiple openings for Python programmers. We are looking for full-time Interns, entry-, mid- and expert-level Python programmers. You could apply here - https://www.linkedin.com/jobs2/view/13795011 We are also looking for Python developers in test - https://www.linkedin.com/jobs2/view/28208583 You could also send us your resume and/or github link to j...@riptideio.com Thank you, Sangeeth ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers