Is Python call by value or call by reference?

preview_player
Показать описание
How are parameters passed to functions in Python?

------------------------------------------------------------

More awesome topics covered here:

------------------------------------------------------------

#python #call-by-value #call-by-reference
Рекомендации по теме
Комментарии
Автор

I was exploring for this the whole Internet, for the simple and precise explanation. luckily found you and you made it all clear. Thank you dude.

varunnadipudi
Автор

very nice explanation...couldn't find out better explanation than this on YouTube..

shivamshinde
Автор

Still i am confused, i am writing a simulator where i am passing objects of various class to various other classes, and then use those objects to assign certain values to variables in that class, it's working but according to you it should not!

ChinmayRGharat
Автор

BASIC - In Python, variables inside functions are considered local if and only if they appear in the left side of an assignment statement. so immutable objects are acts like it its were passed by values

# TODO: acts as a pass by value (IMMUTABLE OBJECTS) but it's passed by reference only
def increment(a):
print('inside function init id(a)', id(a)) # id(a) is same as id(num) so everything is passed by reference only
a += 1 # assignment causes assigning the new value to a ( so a become local to function scope )
print('inside function val of a', a)
print('inside function id(a)', id(a)) # id(a) is not same as id(num) cause of assignment involved
return a


num = 1
print('outside function id(num)', id(num))
increment(num)
print('outside function val of num', num)


But incase of Mutable objects

# TODO: acts as a pass by reference (MUTABLE OBJECTS)
def extend(a):
print('inside function init id(a)', id(a)) # id(a) is same as id(li)
a.append(5) # since no assignment involved any changes to the a( reference of li) also affects the value of the variable outside the function.
print('inside function val of a', a)
print('inside function id(a)', id(a)) # id(a) is same as id(li)
return a


li = [1, 2, 3, 4]
print('outside function id(li)', id(li))
extend(li)
print('outside function val of li', li)

akhils
Автор

Thank you so much I was struggling in this concept and you helped to clear it...🙌

aashitabansal
Автор

Unique information is provided by you, thanks

krishnasoni
Автор

and if in case you seriously intended to empty the list you should:

def clean_it(mylist):
mylist.clear()

swadhikarc
Автор

So basically it's the way the assignment operator works as opposed to member functions (like append) that can mutate the object.

malharjajoo
Автор

Thank you. A very nice explanation, better than many videos.

raviteja
Автор

another important thing += and = are different, list_ += [1] will modify the same object but list_ = list_ + 1 will create new object . This is only true for mutables, for strings/tuples/int which are immutable both will create new objects

arpitjain
Автор

God level explanation very well explained thank you it was helpful.

sujitsutar
Автор

I have a few things to ask.

1. you created a function added a code block that execute a certain operation but where is the output going?
2. Since you haven't returned the output how can you say that your var i.e. num will be incremented just by passing it into a function without getting a return?

codecompass
Автор

Thanks buddy!

Never have thought above it even though using the function calls passing the lists on a daily basis.

mustafabohra
Автор

Crystal Clear bro....you got one more subscriber

tejaskasare
Автор

due seriously perfect explanation....liked it ....thanks

parshvasoni
Автор

I got ur point but can you describe me the 1st example where you use the num variable ....it was not incremented??

niladrighosh
Автор

really liked the way you explained it .Thanks !

SapnaSaini
Автор

Thanks, that was a very good explanation.
Is it true to say then that if a mutable object is passed as a parameter then it is call by reference whereas if an immutable object is passed as a parameter then it is call by value?

sadiqsatwilkar
Автор

So objects are passed by reference, no multiple instances of obj created?

ChinmayRGharat
Автор

Keep rocking bro..fantastic explanation...

subeesha
welcome to shbcf.ru