Changing values of tuple elements - python

I entered the following at the command line:
>>>a = 25
>>>b = 50
>>>t = (a, b)
>>>t
(25, 50)
>>>a = 50
>>>t
(25, 50)
>>>t = (a, b)
>>>t
(50, 50)
Why do I have to reassign the tuple (a, b) to t to see the change in a's value?

This has less to do with tuples and more to do with how assignment works in Python which copies vs references.
This works for other container types (lists) and plain variables.
>>> a = 2
>>> b = [1, a]
>>> a = 7
>>> b
[1, 2]
>>> c = 1
>>> d = c
>>> c = 2
>>> d
1

At first, the value in tuple in python can not be changed. You can only declare a new tuple.
a = 25 means a is a variable. However, the a in t belong to the tuple t. It does not have any relationship with variable a.
The second t = (a, b) as same as t = (50, 50)
Furthermore, you can use id(a) and id(t) to see the difference in your memory address.

You can understand it in this way:-
>>> a =25
>>> b = 50
>>> id(a)
6070712
>>> t = (a, b)
>>> id(t[0])
6070712
>>> a = 50
>>> id(a)
6070412
# When you assigned a = 50, it created new object,
#id(t[0]) and a is not same now, so t is still (25, 50)
This happened because int is immutable, every time you assign a value to it, new object would be created.
Repeat same with mutable type(which can be modified in place)
>>> ls1 = [1,2]
>>> ls2 = [3,4]
>>> t = (ls1, ls2)
>>> ls1[0] = 23
>>> t
([23, 2], [3, 4])
>>> id(ls1)
54696136
>>> id(t[0])
54696136
#here t[0] and ls1 are same object because we could modify ls1 in place
I hope it would help.

It is simple. In Python, the names, such as a and b and t are not objects, they just point to objects. When you enter
>>> a = 25
>>> b = 50
Python sets the name a to point to an int object with value 25 and b to point to int object with value 50.
when you create a tuple with
>>> t = a, b
(no parenthesis required here!) you're telling Python that "please make a new tuple of 2 elements, the first position of which should point to the object that a now points to and the second position should point to the object that b now points to. Actually it would work similarly with a list, or set as well:
>>> l = [a, b]
>>> s = {a, b}
Now the next statement:
>>> a = 50
Means "now, set a to point to an int object with value of 50". The first element of the tuple still continues to point to 25. Actually all assignments to variables behave this way in Python, be the value in a mutable or not:
>>> a = [1, 2]
>>> b = [3, 4]
>>> t = a, b
>>> a = [5, 6]
>>> t
([1, 2], [3, 4])
Even though a points to a mutable value, a list, then a, b means *make a new tuple with first element being the object that a points to at this very moment, and second element being the object that b points to at this very moment; and then a = [5, 6] means *create a new list ... and now make a point to it`. The variables (names) in Python indeed are not "boxes", but they're sort of signs that point to the values.

If we do an assign action like a = b and b is immutable(number, string, tuple etc),assign will make a copy rather than make reference. As a result, modify to the b do not effect a.
For mutable situation:
>>> a = []
>>> b = 3
>>> c = (a, b)
>>> c
([], 3)
>>> a.append(1)
>>> c
((1], 3)

Related

Is list's variable is an object in Python?

a = [1,2,3]
b = [1,2,3]
I understand a is b is false but i can't understand a is [1,2,3] is false
I learned variable is like nickname for objects like x = 2 and id(x) == id(2)
but id(a) is not as same as id(b)...
In this case, a is an object? not a variable?
Variables are references to objects. a does not reference the same object as b. Even though the two objects are the same they have unique addresses in memory and do not depend on each other.
>>> a = b = [1,2,3]
>>> c = [1,2,3]
>>> print(a is b)
True
>>> print(a is c or b is c)
False
>>> a.remove(1)
>>> print(a)
[2, 3]
>>> print(b)
[2, 3]
>>> print(c)
[1, 2, 3]
In the case of the x = 2 and id(x) == id(2) integers are immutable and in CPython id is simply the location of an object in memory. Integers are always the same so storing the same integer several times at different addresses would be a waste of memory.
However, in general DO NOT use integers with is operator as it can lead to different results across different python implementations.
Everything in Python is an object. a is b evaluates to false because, as you know, they are not the same object. They are both instances of the List class, therefore having different locations in memory, and being completely separate in every way except their class structure, or blueprint, as you can think of it. id(a) and id(b) will reflect this, they are not the same object and will therefore not share an ID. This is a reference to their location in memory, and while IDs are not referenced pointers, they are similar in that they describe their not being the same.
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> id(a) == id(b)
False
>>> # the below is an example, every session will result in a different ID,
>>> # as it's being stored at a unique mem location each time
>>> id(a)
2770873780160
>>> id(b)
2770873412800
>>> id(a) - id(b) # simply to show the difference in location.
367360
double equals vs is in python
https://www.tutorialspoint.com/difference-between-and-is-operator-in-python

Is there any way to use python list as reference of original object even with "=" sign?

I hope to achieve the following function with list:
a = 1
b = 2
c = 3
l = []
l.append(a)
l.append(b)
l.append(c)
and if I do:
l[0] = 4
I hope I could get
l = [4, 2, 3]
as well as
a = 4
The difference between this question and many other similar questions is that, I hope to find a way to achieve this with = sign, which in most cases will mean to ask the list set its pointer to another object instead of giving the right hand side value to its current pointing one.
You can't. a=4 points to the integer value 4, which is immutable in python. You cannot change it. You can only change the value of mutable objects, like lists, sets, and dictionaries, plus custom made objects.
You can only do something like:
>>> a = [1]
>>> b = [2]
>>> c = [3]
>>> l = []
>>> l.append(a)
>>> l.append(b)
>>> l.append(c)
>>> l
[[1], [2], [3]]
>>> l[0][0] = 4
>>> a
[4]
>>> l
[[4], [2], [3]]
>>>
Since l[0] = 4 will create a new object, not change a.

Why do I need to copy class instances but not other object types in Python?

I'm a little confused about when I need to explicitly copy an object in Python in order to make changes without altering the original. The Python doc page doesn't have too much detail, and simply says that "assignment statements do not create copies".
Example 1:
>>> a = 4
>>> b = a
>>> b += 1
>>> print(b)
>>> print(a)
# Results are what you'd "expect" them to be:
5
4
Example 2:
>>> class Test():
>>> def __init__(self, x):
>>> self.x = x
>>>
>>> A = Test(4)
>>> B = A
>>> B.x += 1
>>> print(B.x)
>>> print(A.x)
# Unexpected results because B isn't actually a copy of A:
5
5
Why do the two examples behave differently?
Edit: In order to get Example 2 to work, I basically did the following. I'm asking why this isn't necessary for Example 1:
>>> import copy
>>> A = Test(4)
>>> B = copy.copy(A)
>>> B.x += 1
>>> print(B.x)
>>> print(A.x)
a = 4
b = a
..would mean "make b refer to whatever a refers to". That would be 4 in this case. Since integers are immutable, changing b by doing b += 1 would make it point to a different object
Check their ids:
a = 4
b = a
print(id(a)) # Same id
print(id(b)) # Same id
b = 123
print(id(b)) # Different id
Same thing happens with instances A and B when B is not a .copy().
id(object)
Return the “identity” of an object.
An interesting example with mutable objects:
l = [1, 2]
k = l
print(id(l)) # Same id
print(id(k)) # Same id
k.append(3)
print(id(k)) # Same id
print(l, k) # Prints [1, 2, 3] [1, 2, 3]
append() modifies the object both k and l refer to. It doesn't assign k to a different object.
As mentioned by #blcckngth:
k = k + [3]
print(id(k)) # Different id ..
print(l, k) # ..prints [1, 2] [1, 2, 3]
while k += [3] would result in reference to the same object.
In your example 1, you write b += 1. That's the same thing as writing b = b + 1. That uses the = operator to assign a value to b. You're just changing the value that the name b refers to. In example 2, you're changing an attribute of the object that both names (A and B) refer to. They still both refer to the same object in memory, so changing the .x attribute of that object changes both B.x and A.x.

Python assigning multiple variables to same value? list behavior

I tried to use multiple assignment as show below to initialize variables, but I got confused by the behavior, I expect to reassign the values list separately, I mean b[0] and c[0] equal 0 as before.
a=b=c=[0,3,5]
a[0]=1
print(a)
print(b)
print(c)
Result is:
[1, 3, 5]
[1, 3, 5]
[1, 3, 5]
Is that correct? what should I use for multiple assignment?
what is different from this?
d=e=f=3
e=4
print('f:',f)
print('e:',e)
result:
('f:', 3)
('e:', 4)
If you're coming to Python from a language in the C/Java/etc. family, it may help you to stop thinking about a as a "variable", and start thinking of it as a "name".
a, b, and c aren't different variables with equal values; they're different names for the same identical value. Variables have types, identities, addresses, and all kinds of stuff like that.
Names don't have any of that. Values do, of course, and you can have lots of names for the same value.
If you give Notorious B.I.G. a hot dog,* Biggie Smalls and Chris Wallace have a hot dog. If you change the first element of a to 1, the first elements of b and c are 1.
If you want to know if two names are naming the same object, use the is operator:
>>> a=b=c=[0,3,5]
>>> a is b
True
You then ask:
what is different from this?
d=e=f=3
e=4
print('f:',f)
print('e:',e)
Here, you're rebinding the name e to the value 4. That doesn't affect the names d and f in any way.
In your previous version, you were assigning to a[0], not to a. So, from the point of view of a[0], you're rebinding a[0], but from the point of view of a, you're changing it in-place.
You can use the id function, which gives you some unique number representing the identity of an object, to see exactly which object is which even when is can't help:
>>> a=b=c=[0,3,5]
>>> id(a)
4473392520
>>> id(b)
4473392520
>>> id(a[0])
4297261120
>>> id(b[0])
4297261120
>>> a[0] = 1
>>> id(a)
4473392520
>>> id(b)
4473392520
>>> id(a[0])
4297261216
>>> id(b[0])
4297261216
Notice that a[0] has changed from 4297261120 to 4297261216—it's now a name for a different value. And b[0] is also now a name for that same new value. That's because a and b are still naming the same object.
Under the covers, a[0]=1 is actually calling a method on the list object. (It's equivalent to a.__setitem__(0, 1).) So, it's not really rebinding anything at all. It's like calling my_object.set_something(1). Sure, likely the object is rebinding an instance attribute in order to implement this method, but that's not what's important; what's important is that you're not assigning anything, you're just mutating the object. And it's the same with a[0]=1.
user570826 asked:
What if we have, a = b = c = 10
That's exactly the same situation as a = b = c = [1, 2, 3]: you have three names for the same value.
But in this case, the value is an int, and ints are immutable. In either case, you can rebind a to a different value (e.g., a = "Now I'm a string!"), but the won't affect the original value, which b and c will still be names for. The difference is that with a list, you can change the value [1, 2, 3] into [1, 2, 3, 4] by doing, e.g., a.append(4); since that's actually changing the value that b and c are names for, b will now b [1, 2, 3, 4]. There's no way to change the value 10 into anything else. 10 is 10 forever, just like Claudia the vampire is 5 forever (at least until she's replaced by Kirsten Dunst).
* Warning: Do not give Notorious B.I.G. a hot dog. Gangsta rap zombies should never be fed after midnight.
Cough cough
>>> a,b,c = (1,2,3)
>>> a
1
>>> b
2
>>> c
3
>>> a,b,c = ({'test':'a'},{'test':'b'},{'test':'c'})
>>> a
{'test': 'a'}
>>> b
{'test': 'b'}
>>> c
{'test': 'c'}
>>>
In python, everything is an object, also "simple" variables types (int, float, etc..).
When you changes a variable value, you actually changes it's pointer, and if you compares between two variables it's compares their pointers.
(To be clear, pointer is the address in physical computer memory where a variable is stored).
As a result, when you changes an inner variable value, you changes it's value in the memory and it's affects all the variables that point to this address.
For your example, when you do:
a = b = 5
This means that a and b points to the same address in memory that contains the value 5, but when you do:
a = 6
It's not affect b because a is now points to another memory location that contains 6 and b still points to the memory address that contains 5.
But, when you do:
a = b = [1,2,3]
a and b, again, points to the same location but the difference is that if you change the one of the list values:
a[0] = 2
It's changes the value of the memory that a is points on, but a is still points to the same address as b, and as a result, b changes as well.
Yes, that's the expected behavior. a, b and c are all set as labels for the same list. If you want three different lists, you need to assign them individually. You can either repeat the explicit list, or use one of the numerous ways to copy a list:
b = a[:] # this does a shallow copy, which is good enough for this case
import copy
c = copy.deepcopy(a) # this does a deep copy, which matters if the list contains mutable objects
Assignment statements in Python do not copy objects - they bind the name to an object, and an object can have as many labels as you set. In your first edit, changing a[0], you're updating one element of the single list that a, b, and c all refer to. In your second, changing e, you're switching e to be a label for a different object (4 instead of 3).
You can use id(name) to check if two names represent the same object:
>>> a = b = c = [0, 3, 5]
>>> print(id(a), id(b), id(c))
46268488 46268488 46268488
Lists are mutable; it means you can change the value in place without creating a new object. However, it depends on how you change the value:
>>> a[0] = 1
>>> print(id(a), id(b), id(c))
46268488 46268488 46268488
>>> print(a, b, c)
[1, 3, 5] [1, 3, 5] [1, 3, 5]
If you assign a new list to a, then its id will change, so it won't affect b and c's values:
>>> a = [1, 8, 5]
>>> print(id(a), id(b), id(c))
139423880 46268488 46268488
>>> print(a, b, c)
[1, 8, 5] [1, 3, 5] [1, 3, 5]
Integers are immutable, so you cannot change the value without creating a new object:
>>> x = y = z = 1
>>> print(id(x), id(y), id(z))
507081216 507081216 507081216
>>> x = 2
>>> print(id(x), id(y), id(z))
507081248 507081216 507081216
>>> print(x, y, z)
2 1 1
in your first example a = b = c = [1, 2, 3] you are really saying:
'a' is the same as 'b', is the same as 'c' and they are all [1, 2, 3]
If you want to set 'a' equal to 1, 'b' equal to '2' and 'c' equal to 3, try this:
a, b, c = [1, 2, 3]
print(a)
--> 1
print(b)
--> 2
print(c)
--> 3
Hope this helps!
What you need is this:
a, b, c = [0,3,5] # Unpack the list, now a, b, and c are ints
a = 1 # `a` did equal 0, not [0,3,5]
print(a)
print(b)
print(c)
Simply put, in the first case, you are assigning multiple names to a list. Only one copy of list is created in memory and all names refer to that location. So changing the list using any of the names will actually modify the list in memory.
In the second case, multiple copies of same value are created in memory. So each copy is independent of one another.
The code that does what I need could be this:
# test
aux=[[0 for n in range(3)] for i in range(4)]
print('aux:',aux)
# initialization
a,b,c,d=[[0 for n in range(3)] for i in range(4)]
# changing values
a[0]=1
d[2]=5
print('a:',a)
print('b:',b)
print('c:',c)
print('d:',d)
Result:
('aux:', [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])
('a:', [1, 0, 0])
('b:', [0, 0, 0])
('c:', [0, 0, 0])
('d:', [0, 0, 5])
To assign multiple variables same value I prefer list
a, b, c = [10]*3#multiplying 3 because we have 3 variables
print(a, type(a), b, type(b), c, type(c))
output:
10 <class 'int'> 10 <class 'int'> 10 <class 'int'>
Initialize multiple objects:
import datetime
time1, time2, time3 = [datetime.datetime.now()]*3
print(time1)
print(time2)
print(time3)
output:
2022-02-25 11:52:59.064487
2022-02-25 11:52:59.064487
2022-02-25 11:52:59.064487
E.g: basically a = b = 10 means both a and b are pointing to 10 in the memory, you can test by id(a) and id(b) which comes out exactly equal to a is b as True.
is matches the memory location but not its value, however == matches the value.
let's suppose, you want to update the value of a from 10 to 5, since the memory location was pointing to the same memory location you will experience the value of b will also be pointing to 5 because of the initial declaration.
The conclusion is to use this only if you know the consequences otherwise simply use , separated assignment like a, b = 10, 10 and won't face the above-explained consequences on updating any of the values because of different memory locations.
The behavior is correct. However, all the variables will share the same reference. Please note the behavior below:
>>> a = b = c = [0,1,2]
>>> a
[0, 1, 2]
>>> b
[0, 1, 2]
>>> c
[0, 1, 2]
>>> a[0]=1000
>>> a
[1000, 1, 2]
>>> b
[1000, 1, 2]
>>> c
[1000, 1, 2]
So, yes, it is different in the sense that if you assign a, b and c differently on a separate line, changing one will not change the others.
Here are two codes for you to choose one:
a = b = c = [0, 3, 5]
a = [1, 3, 5]
print(a)
print(b)
print(c)
or
a = b = c = [0, 3, 5]
a = [1] + a[1:]
print(a)
print(b)
print(c)

Assigning a value to an element of a slice in Python

This is a simple question about how Python handles data and variables. I've done a lot of experimenting and have Python mostly figured out, except this keeps tripping me up:
[edit: I separated and rearranged the examples for clarity]
Example 1:
>>> a = [[1], 2]
>>> a[0:1]
[[1]]
>>> a[0:1] = [[5]]
>>> a
[[5], 2] # The assignment worked.
Example 2:
>>> a = [[1], 2]
>>> a[0:1][0]
[1]
>>> a[0:1][0] = [5]
>>> a
[[1], 2] # No change?
Example 3:
>>> a = [[1], 2]
>>> a[0:1][0][0]
1
>>> a[0:1][0][0] = 5
>>> a
[[5], 2] # Why now?
Can anybody explain to me what's going on here?
So far the answers seem to claim that a[0:1] returns a new list containing a reference to the first element of a. But I don't see how that explains Example 1.
a[0:1] is returning a new array which contains a reference to the array [1], thus you end up modifying the inner array via a reference call.
The reason the first case doesn't modify the [1] array is that you're assigning the copied outer array a new inner array value.
Bottom line - a[0:1] returns a copy of the data, but the inner data is not copied.
My understanding is slicing returns a new object. That is it's return value is a new list.
Hence you can not use an assignment operator to changes the values of the original list
>>> a = [[1], 2, 3]
>>> k = a[0:2]
>>> id(a)
4299352904
>>> id(k)
4299353552
>>>
>>> id(a)
4299352904
>>> id(a[0:2])
4299352832
some more plays along the lines
>>> k = 5
>>>
>>> id(k)
4298182344
>>> a[0] = [1,2]
>>> a
[[1, 2], 2, 3]
>>> id(a)
4299352904
>>>
[Edit: on second part of question]
>>> a[0:1] = [[5]]
The following notation is also called commonly as slice assignment
The behavior for builtin lists is atomic (delete + insert) happens in one go. My understanding is that this is not allowed for custom sequence.
There are three distinct operations with indices, all are translated to method calls:
a[i] = b => a.__setitem__(i, b)
del a[i] => a.__delitem__(i)
a[i] used as an expression => a.__getitem__(i)
Here a, b and i are expressions, and i can contain slice objects created using the colon shorthand syntax. E.g.:
>>> class C(object):
... def __setitem__(self, *a):
... print a
...
>>> C()[1] = 0
(1, 0)
>>> C()['foo'] = 0
('foo', 0)
>>> C()['foo':'bar'] = 0
(slice('foo', 'bar', None), 0)
>>> C()['foo':'bar',5] = 0
((slice('foo', 'bar', None), 5), 0)
So what's happening in your third example is this:
a[0:1][0][0] = 5
becomes
a.__getitem__(slice(0,1)).__getitem__(0).__setitem__(0, 5)
The first __getitem__ returns a copy of part of the list, but the second __getitem__ returns the actual list inside that, which is then modified using __setitem__.
Your second example on the other hand becomes
a.__getitem__(slice(0,1)).__setitem__(0, 5)
So __setitem__ is being called on the sliced copy, leaving the original list intact.

Categories