pass by value and reference in python

preview_player
Показать описание
how to pass a variable by value and reference in python
Рекомендации по теме
Комментарии
Автор

Python do not pass arguments by value: all by reference. But ... since python has two types of objects: immutable and mutable, here is what happens:

Immutable arguments are effectively passed by value: string, integer, tuple are all passed by reference. But since you can't change them in-place inside the function it looks as if it is passed by value.

Mutable arguments are effectively passed by reference: lists or dictionaries are passed by its pointers. Any in-place change inside the function like (append or del) will affect the original object.

easycoding
Автор

No dude. Python works differently than your old C and C++. By default, any kind of argument passed is taken by reference. Always. That's to save time from copying the data. The 'x' of your function (formal argument) is simply an alias that points to the same memory location as your original 'x' (actual argument). However, if you make changes to the formal argument variable, then that it starts pointing to a different location with that changed value. The only reason the list method is working is because of this fact only. Here, you're just telling Python to change the value at some index, not assign a totally new value to the variable. Hence, since it was passed by reference, and the change doesn't lead to pointing to a different memory, the change is reflected back in your actual argument as well.

comradepeter
Автор

😯😭😭thanks...i was in trouble... your explanation was awesome!! Actually got the concept

internetuser
Автор

Question: but in both scenarios, we are changing the variable we passed inside the function. Then why did the original value of x not change but the original value of a list changed. The code is exactly the same, except x was an integer data variable and a was a list. Sorry, im a bit confused :(

MridulaCh
Автор

You explained it wrong. Completely wrong python by default passes reference, you have to make it pass by value .

niteshraut
Автор

y pass by reference works on list but not on variables?

dharmendrapradhan
Автор

If you pass a list it is by default call by reference...

Nothing I have learned from your video..

sanjoy
Автор

isn't pass by reference vs value a property of the language? how can you have both

miguelpetrarca
Автор

thank you so much sir . pls keep making videos

harishsiddharth
Автор

Nice one bro . But improve your speaking!

naraendrareddy
Автор

bro you gotta speak up...But good explantion I guess

squad
join shbcf.ru