Sunday, December 2, 2018

list - Why different reference is created for int object when passed to function in python?

As per my knowledge the Python interpreter creates references from 0 to 256 for int objects, at the start itself. Here I got output 3,[44,2,3], for the list I understood, but why did the same thing not happen with the int object? I expected the output to be 1,[44,2,3]. Can anyone please explain this behavior to me? I use Python 3.


def change(var, lst):
var = 1
lst[0] = 44
k = 3
a = [1, 2, 3]
change(k, a)
print(k)
print(a)

Output:


3
[44,2,3]

Expected output:


1
[44,2,3]

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...