What happens in memory while unpacking a collection? - python

Say
list1=[4,8,12]
a,b,c=list1
output is a=4,b=8,c=12.
My confusion
Instructor told us that it is not like a gets mapped to 4, b to 8, and c to 12. I didn't understand what he told clearly (although I listened repeatedly to him multiple times). He was telling something like object is created for 4 and a is mapped to 4. But what is the difference between this and what I have presented below in figure?

The thing about your picture that's misleading is that it implies that a, b, and c reference slices of list1. If you change list1, though, you will find that a, b, and c aren't affected by that change.
A better way to draw the picture might be to show 4, 8, and 12 separate from list1:
list1-->[ ][ ][ ]
| | |
V V V
4 8 12
^ ^ ^
| | |
a b c
All of the variables are independent of one another, even though some of them (e.g. list1[0] and a) currently point to the same values.
To put it another way: saying a = list1[0] is saying "evaluate list1[0] and assign a to reference whatever that value is right now", which is not the same as saying "make a be an alias for list1[0]".

Try this:
# define the list
list1=[4,8,12]
# reserve 3 memory spaces and unpack the values from list into them
# those memory spaces will contain one integer each one of the size of
# sys.getsizeof(int()) == 28 bytes (Python 3)
# a,b and c are actually pointers to those memory spaces
a,b,c=list1
print(a,b,c)
# change the first value of the list
list1[0] = 56
print(list1)
# now check that indeed "a" is not the same pointer than "list1[0]"
print(a)
But
You must to be careful with this kind of asignations with lists, try also this:
list2 = list1
print(list1, list2)
# then change any of them
list1 [0] = -1
# check that "list2" is pointing to the same memory address than "list1"
print(list1, list2)

Related

I'm trying to understand how reference works in python

After line 7, I haven't written a single line of code which mentions the list named 'outer'. However, if you execute it, you'll see that the 'outer' (i.e, the nested lists inside it) list would change/update due to lines 10 and 12...
I'm guessing it has something to do with reference vs value. My question is, why didn't line 13 effect (change/update) the 'outer' list the same way that lines 7 and 10 did? I'm trying to undertand this concept. How do I go about it. I know there's a lot of resources online.. but I don't even know what to google. Please help.
inner = []
outer = []
lis = ['a', 'b', 'c']
inner.append(lis[0])
outer.append(inner) <<---- Line 7 <<
print(outer)
inner.append(lis[1]) <<---- Line 10 <<
print(outer)
inner.append(lis[2]) <<---- Line 12 <<
print(outer)
lis[2] = 'x' <<---- Line *******13******* <<
print(outer)
This is a boiled-down version of your example:
some_list = []
a = 2
some_list.append(a)
a = 3
print(some_list) # output: [2]
print(a) # output: 3
If we follow your original logic, you would expect some_list to contain the value 3 when we print it. But the reality is that we never appended a itself to the list. Instead, writing some_list.append(a) means appending the value referenced by a to the list some_list.
Remember, variables are simply references to a value. Here's the same snippet as above, but with an explanation of what's referencing what.
some_list = [] # the name "some_list" REFERENCES an empty list
a = 2 # the name "a" REFERENCES the integer value 2
some_list.append(a) # we append the value REFERENCED BY "a"
# (the integer 2) to the list REFERENCED
# BY "some_list". That list is not empty
# anymore, holding the value [2]
a = 3 # the name "a" now REFERENCES the integer value 3. This
# has no implications on the list REFERENCED BY "some_list".
# We simply move the "arrow" that pointed the name "a" to
# the value 2 to its new value of 3
print(some_list) # output: [2]
print(a) # output: 3
The key aspect to understand here is that variables are simply references to a value. Writing some_list.append(a) does not mean "place the variable a into the list" but rather "place the value that the variable a references at this moment in time into the list". Variables cannot keep track of other variables, only the values that they are a reference to.
This becomes even clearer if we append to some_list a second time, after modifying the value that a references:
some_list = []
a = 2
some_list.append(a)
a = 3
some_list.append(a)
print(some_list) # output: [2, 3]
print(a) # output: 3
In Python, when you store a list in variable you don't store the list itself, but a reference to a list somewhere in the computer's RAM. If you say
a = [0, 1, 2]
b = a
c = 3
then both a and b will be references to the same list as you set b to a, which is a reference to a list. Then, modifying a will modify b and vice-versa. c, however, is an integer; it works differently. It's like that:
┌───┐
a │ █━┿━━━━━━━━━━━━━━━┓ ┌───┬───┬───┐
└───┘ ┠→│ 0 │ 1 │ 2 │
┌───┐ ┃ └───┴───┴───┘
b │ █━┿━━━━━━━━━━━━━━━┛
└───┘
┌───┐
c │ 3 │
└───┘
a and b are references to a same list, but c is an pointer to an integer which is copied (the integer) when you say d = c. The reference to it, however, is not copied.
So, let's go back to your program. When you say inner.append(lis[n]) you add the value at the end of the list inner. You don't add the reference to the item #2 of the list lis but you create a copy of the value itself and add to the list the reference to this copy!
If you modify lis, then it will have an impact only on variables that are references to lis.
If you want inner to be modified if you modify lis, then replace the inner.append(lis[n])s by inner.append(lis).

how del list[:] works [duplicate]

This question already has answers here:
Clarify how python del works for lists and slices
(2 answers)
Closed 3 years ago.
list[:] creates a copy of the list then why does del list[:] remove all the list items?
Shouldn't it delete the copy of the list?
The answer is no, it shouldn't. It's intended to delete all of the elements of the list. Looking at the documentation, the result of s.clear() (where s is a mutable sequence type, for example a list) is:
removes all items from s (same as del s[:])
Hence, del s[:] is the same as s.clear() in that it removes all items from s.
Perhaps, this is a bit more understandable if you consider that the function called behind the scenes is __delitem__. From the docs:
Called to implement deletion of self[key]. Same note as for __getitem__(). This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper key values as for the __getitem__() method.
Consider the following difference:
a = [1,2,3]
b = a
del a
# print(a) ## raises an error
print(b) ## prints [1,2,3]
c = [1,2,3]
d = c
del c[:]
print(c) ## prints []
print(d) ## prints []
So why would you want del a[:] to behave this way? Well, think of it as just a special case of deleting a slice of a list. For example, say that you'd want to delete the 3rd, 4th, and 5th element of a long list a = list(range(40)). With the slice notation and the __delitem__ this is easy, just use del a[3:6]. Now try to do the same with a for loop and you'll soon find out it can get quite cumbersome. Heck, just try to delete all the items of a (but not the a itself!) with a for loop ;)
Because a[:] is is simply just a copy of a, consisting of the identical objects as a but a different object than a. Their elements are identical but they are not.
Let's create a list and do some id checks:
a = [1, 2, 3]
print(id(a)) # 97731118088
print(id(a[:])) # 97731213576
print(id(a[:])) # 97731212104
print(id(a[:])) # 97731198600
Notice the changing ids of the copy a[:]. It is an object that is created on the fly every time it is called, and most importantly, even a[:] is a[:] not True! Let's look at the ids of their elements to come to a conclusion:
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
So, we can conclude that a[:] is an object that consists of the very elements of a but is a different object than a, and is a different object every time it is called. The elements of a and a[:] are all identical, but they themselves are not.
So what del a[:] does is to remove all of the elements of a. That way a is mutated, and you end up with an empty a, i.e. []. However, del a removes the name a from namespace completely, and when you ask Python to print a for you, you'll get a NameError: name 'a' is not defined.
But how do we know that? Well, let's gain some perspective by disassembling del on a vs a[:]:
Let's define two functions:
def deletion1(a):
del a
def deletion2(a):
del a[:]
Let's disassemble them:
import dis
dis.dis(x = deletion1)
1 0 DELETE_FAST 0 (a)
2 LOAD_CONST 0 (None)
4 RETURN_VALUE
dis.dis(x = deletion2)
1 0 LOAD_FAST 0 (a)
2 LOAD_CONST 0 (None)
4 LOAD_CONST 0 (None)
6 BUILD_SLICE 2
8 DELETE_SUBSCR
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
The dis documentation indicates that the DELETE_FAST operation, which the first function does, simply "Deletes local co_varnames[var_num]". This is basically removal of that name a so that you can't reach the list object anymore. Beware, this does not remove the object that is referred to by the name a, but just removes its name so that the a is not a reference to anything anymore. The object 97731118088 is still the same list, [1, 2, 3]:
import gc
for obj in gc.get_objects():
if id(obj) == 97731118088:
print(obj)
# [1, 2, 3]
On the other hand, again from the documentation, DELETE_SUBSCR "Implements del TOS1[TOS]", which is basically "an in-place operator that removes the top of the stack and pushes the result back on the stack". This way, the stack elements are removed, and you are left with the name a, which now refers to a list whose elements are deleted, i.e. just an "empty shell", if you will. After this operation, a becomes [], but has still the same id of 97731118088. Just its elements are gone via an in-place deletion.

why is my for loop printing 2 at each iteration if b has not even been defined as a placeholder in the for loop declaration?

I am fairly new to python and I just learned about tuple unpacking, so I was playing around with this concept and got a weird result.
nList = [(1,2),4,5,6]
for a in nList:
print(a)
print(b)
I was expecting my program to crash since b is not defined as a placeholder and even if it was only the first element of my list is a tuple, but instead, I got the following result:
(1, 2)
2
4
2
5
2
6
2
You have not initialized the variable b in your code, because of which it is using some garbage value of b from memory (which is 2). To avoid such scenarios you must initialize your variables with some value (or 0) in start of your code before using them.

While-loop Python Fibonacci

I am new to python and I have really poor expiriences with other codes.
For the most of you a stupid question but somewhere I should start.
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
I don't understand why one should enter a, b = b, a+b
I see and understand the result and I can conclude the basic algorithm but I don't get the real understanding of what is happening with this line and why we need it.
Many thanks
This line is executed in the following order:
New tuple is created with first element equal to b and second to a + b
The tuple is unpacked and first element is stored in a and the second one in b
The tricky part is that the right part is executed first and you do not need to use temporary variables.
The reason you need it is because, if you update a with a new value, you won't be able to calculate the new value of b. You could always use temporary variables to keep the old value while you calculate the new values, but this is a very neat way of avoiding that.
It's called sequence unpacking.
In your statement:
a, b = b, a + b
the right side b, a + b creates a tuple:
>>> 8, 5 + 8
(8, 13)
You then assign this to the left side, which is also a tuple a, b.
>>> a, b = 8, 13
>>> a
8
>>> b
13
See the last paragraph the documentation on Tuples and Sequences:
The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:
>>> x, y, z = t
This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.

Python Referenced For Loop

I'm playing with for loops in Python and trying to get used to the way they handle variables.
Take the following piece for code:
a=[1,2,3,4,5]
b=a
b[0]=6
After doing this, the zeroth element of both b and a should be 6. The = sign points a reference at the array, yes?
Now, I take a for loop:
a=[1,2,3,4,5]
for i in a:
i=6
My expectation would be that every element of a is now 6, because I would imagine that i points to the elements in a rather than copying them; however, this doesn't seem to be the case.
Clarification would be appreciated, thanks!
Everything in python is treated like a reference. What happens when you do b[0] = 6 is that you assign the 6 to an appropriate place defined by LHS of that expression.
In the second example, you assign the references from the array to i, so that i is 1, then 2, then 3, ... but i never is an element of the array. So when you assign 6 to it, you just change the thing i represents.
http://docs.python.org/reference/datamodel.html is an interesting read if you want to know more about the details.
That isn't how it works. The for loop is iterating through the values of a. The variable i actually has no sense of what is in a itself. Basically, what is happening:
# this is basically what the loop is doing:
# beginning of loop:
i = a[0]
i = 6
# next iteration of for loop:
i = a[1]
i = 6
# next iteration of for loop:
i = a[2]
i = 6
# you get the idea.
At no point does the value at the index change, the only thing to change is the value of i.
You're trying to do this:
for i in xrange(len(a)):
a[i] = 6 # assign the value at index i
Just as you said, "The = sign points a reference". So your loop just reassigns the 'i' reference to 5 different numbers, each one in turn.

Categories

Resources