yesdeepakmittal / pytraining

High level compact overview of Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pytraining

High Level Compact Overview of Advance Python


Interview Q & A for Advance Python


Q: Do Objects copied again in Python or same Objects passes as an Argument to the function?

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]

Blogs & Other resources

About

High level compact overview of Python


Languages

Language:Jupyter Notebook 99.9%Language:Python 0.1%Language:Java 0.0%