High Level Compact Overview of Advance Python
In Python, Objects do not copied again. For mutuable data types, we can update the Object right in the calling function and we even need not to return it explicitly if there is no brokage of Alaising.
def test_fn(arr):
print(id(arr)) # arr is the same argument object
arr += [101] #📌 Alias not broken + Addition of the new ele at same memory add.
print(id(arr)) # object is added at memory location | No need to return anything
test_arr = [1,2,3,4,5]
print(id(test_arr)) # id of array before passing into the function
test_fn(test_arr)
print(test_arr) # Even without returning anything, this array is updated
Output:
139928806989408
139928806989408
139928806989408
[1, 2, 3, 4, 5, 101]
def test_fn(arr):
print(id(arr)) # arr is the same argument object
arr = arr + [101] #📌 Alias has broken now
print(id(arr)) # after broken of alias, arr needed to return now
test_arr = [1,2,3,4,5]
print(id(test_arr)) # id of array before passing into the function
test_fn(test_arr)
print(test_arr) # Even without returning anything, this array is updated
Output:
139928716393440
139928716393440
139928807003312
[1, 2, 3, 4, 5]