Is Python call by value or call by reference?

Поделиться
HTML-код
  • Опубликовано: 28 сен 2024
  • How are parameters passed to functions in Python?
    ------------------------------------------------------------
    Explore my tutorials: nikhilkumarsin...
    More awesome topics covered here:
    WhatsApp Bot using Twilio and Python : tinyurl.com/y9...
    Serverless Rest API using AWS and Python : tinyurl.com/y8...
    Creating Chat Application using Flask, Socket.IO & mongoDB : tinyurl.com/y8...
    Curses in Python : tinyurl.com/y8...
    Discovering Hidden APIs : tinyurl.com/y7...
    RegEx in Python : tinyurl.com/y8...
    Python for Data Science : tinyurl.com/yd...
    Introduction to Pandas : tinyurl.com/yc...
    Introduction to Matplotlib : tinyurl.com/y8...
    Introduction to Numpy : tinyurl.com/yc...
    Functional Programming in Python : tinyurl.com/yb...
    Python Package Publishing : tinyurl.com/ya...
    Multithreading in Python : tinyurl.com/yc...
    Multiprocessing in Python : tinyurl.com/y8...
    Parallel Programming in Python : tinyurl.com/ya...
    Concurrent Programming in Python : tinyurl.com/y9...
    Dataclasses in Python : tinyurl.com/y8...
    Exploring RUclips Data API : tinyurl.com/y7...
    Just For Fun : tinyurl.com/y9...
    Exploring AWS : tinyurl.com/yd...
    Jupyter Notebook (Tips, Tricks and Hacks) : tinyurl.com/y8...
    Decorators in Python : tinyurl.com/y7...
    Inside Python : tinyurl.com/yd...
    Exploring datetime : tinyurl.com/y9...
    Collections in Python : tinyurl.com/yb...
    Networking : tinyurl.com/y9...
    Computer Vision for noobs : tinyurl.com/y8...
    Python for web : tinyurl.com/y8...
    Awesome Linux Terminal : tinyurl.com/y8...
    Intermediate Python : tinyurl.com/ya...
    Tips, tricks, hacks and APIs : tinyurl.com/y9...
    Optical Character Recognition : tinyurl.com/yb...
    Facebook Messenger Bot Tutorial : tinyurl.com/y9...
    Facebook: / indianpythonista
    Github: www.github.com...
    Twitter: / nikhilksingh97
    ------------------------------------------------------------
    #python #call-by-value #call-by-reference

Комментарии • 73

  • @sastavideoswala
    @sastavideoswala 5 лет назад +4

    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!

  • @varunnadipudi2257
    @varunnadipudi2257 4 года назад +13

    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.

  • @code2compass
    @code2compass 7 месяцев назад

    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?

  • @niladrighosh5518
    @niladrighosh5518 4 года назад

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

  • @malharjajoo7393
    @malharjajoo7393 4 года назад +2

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

  • @mustafabohra2070
    @mustafabohra2070 3 года назад

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

  • @arpitjain3467
    @arpitjain3467 2 года назад

    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

  • @Dr.HarshTruth
    @Dr.HarshTruth 2 года назад +1

    Great video! Simple and clear!

  • @sujitsutar3271
    @sujitsutar3271 2 года назад

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

  • @thorunitha7755
    @thorunitha7755 5 лет назад +1

    How we got my list as 1,2,3,4,5,5?

  • @tejaskasare1858
    @tejaskasare1858 4 года назад

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

  • @shantomathew-fh3hv
    @shantomathew-fh3hv 5 лет назад

    integer passed in the first example is also an object right? so whats the difference between the integer object and list object? its not clear.

    • @artuncurax
      @artuncurax 5 лет назад +2

      i think there is no difference. you pass num to the function, so a is referencing to same "1"now. But the moment you increment it, a starts to reference "2" object, because everything is object. dont you think so?

    • @kushagra2255
      @kushagra2255 5 лет назад +2

      As list are mutable objects and integers & strings are immutable. So when you change the inc value of variable inside a function it actually makes a new container for it. But in case of list when you append something it does on same container. So calling that variable/list outside the function will give different result. In short python uses call-by-reference but if you pass immutable arguments like integers, strings or tuples to a function , the passing acts like call-by-value.

  • @venkateshk3079
    @venkateshk3079 4 года назад

    Clean and professional explanation. I loved it. May I know which software you are using to execute the python statements??

    • @nope4881
      @nope4881 4 года назад

      Jupyter Notebook

  • @artibhasme8971
    @artibhasme8971 5 лет назад

    i believe this is more related to mutable and immutable types in python. Immutable are call by value and mutable are call by ref. plz correct me if my understanding is not correct

    • @IndianPythonista
      @IndianPythonista  5 лет назад

      No relation of function calls with an object being mutable or immutable. :)

  • @johnnyivanovich-nunez8028
    @johnnyivanovich-nunez8028 4 года назад

    Very helpful, thank you.

  • @akashmacha1999
    @akashmacha1999 5 лет назад +1

    Dope explanation brother! 🔥

  • @mertyertugrul
    @mertyertugrul 5 лет назад +2

    Nice work...

  • @heartfeltrhythm3378
    @heartfeltrhythm3378 5 лет назад

    Very clear explanation! Thank you :)

  • @worldisstrange2569
    @worldisstrange2569 4 года назад

    Python supports call by object

  • @DeepakKumar-wu4dt
    @DeepakKumar-wu4dt 3 года назад

    def add_more(l):
    l.clear( ) # this won't change the id
    l = [ ] # this change the id of l
    return
    mylist = [1,2,3]
    add_more(mylist)

  • @anubhavtyagi4359
    @anubhavtyagi4359 3 года назад

    after 6:06 i am even more Confused, Incomplete Video

  • @saurav4180
    @saurav4180 5 лет назад +8

    crystal clear , pretty cool..!

  • @swadhikarc7858
    @swadhikarc7858 5 лет назад +3

    and if in case you seriously intended to empty the list you should:
    def clean_it(mylist):
    mylist.clear()

  • @sadiqsatwilkar
    @sadiqsatwilkar 4 года назад +3

    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?

    • @sachingore7491
      @sachingore7491 4 года назад +1

      this is exactly what I also deduced from the video but needs an affirmation on it. Let me know if you get any answers :)

    • @chrisaes3235
      @chrisaes3235 3 года назад

      I suspect, that it is about primitive data types like int (which are immutable) and objects (which can be immutable as well, though, if you tell them to be).
      Please correct me, if I'm wrong, I'm quite new to coding.

  • @dishantsharma8614
    @dishantsharma8614 6 лет назад +5

    This deserves more views.

  • @srujanchalavadi
    @srujanchalavadi Год назад

    Good Explanation

  • @hlearningkids
    @hlearningkids 3 года назад

    😎 coolest vid.

  • @coffeepen106
    @coffeepen106 2 года назад

    Fantastic

  • @raviteja5125
    @raviteja5125 5 лет назад +1

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

  • @samarthbhandari1360
    @samarthbhandari1360 5 лет назад +1

    So by the logic you explained, shouldn't a in the first example also return 2 instead of 1? Since a is the same object and you're just incrementing it by 1, or is that interpreted as a different assignment?

    • @IndianPythonista
      @IndianPythonista  5 лет назад +1

      In Python, a variable name is just a reference to any object. Here, 1 and 2 are two different Int objects. Hence change in local scoped a in function makes it point to a different object. The global scoped a still refers to original object, I.e. 1

    • @593ramya
      @593ramya 5 лет назад +1

      It will always create a new object when you increment it using a+=1; You can just check this :
      a=1
      id(a)
      a+=1
      id(a)
      Both id's will be different, which means it will create a new object all together

    • @kushagra2255
      @kushagra2255 5 лет назад +6

      As list are mutable objects and integers & strings are immutable. So when you change the inc value of variable inside a function it actually makes a new container for it. But in case of list when you append something it does on same container. So calling that variable/list outside the function will give different result. In short python uses call-by-reference but if you pass immutable arguments like integers, strings or tuples to a function , the passing acts like call-by-value.

    • @ashutoshpanigrahy7326
      @ashutoshpanigrahy7326 2 года назад

      @@kushagra2255 Nice!

  • @akhils3561
    @akhils3561 2 года назад

    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)

  • @krishnasoni5883
    @krishnasoni5883 5 лет назад +4

    Unique information is provided by you, thanks

  • @aashitabansal7654
    @aashitabansal7654 11 месяцев назад

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

  • @shivamshinde9904
    @shivamshinde9904 3 года назад

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

  • @explors91
    @explors91 3 года назад

    can you demonstrate call by reference without using list, just use simple variable other than list. Is it possible?

  • @birajitnath4235
    @birajitnath4235 5 лет назад +1

    thank you..But what about with those primitive types???

    • @IndianPythonista
      @IndianPythonista  5 лет назад

      Concept of call by value and call by reference is generic and the concept works same for all types of data. Please ask if you have any special case or example where you are getting unexpected output.

    • @onePunch95
      @onePunch95 5 лет назад +2

      say you pass x = 1, to f(y), now if you increment y, y+=1, y points to new object 2, and x is still pointing to 1

    • @avdheshyadav8416
      @avdheshyadav8416 4 года назад

      @@onePunch95 Please Clear this

  • @ananyakumari6807
    @ananyakumari6807 3 года назад

    Key word here is assignment, use of assignment creates a new object.

  • @codesetup265
    @codesetup265 3 года назад

    what editor do you use?

  • @sastavideoswala
    @sastavideoswala 5 лет назад

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

  • @bouhannacheabdallah
    @bouhannacheabdallah 5 лет назад

    But i tried the same thing under python version 3 and it doesnt go the same

  • @tejasiddani9136
    @tejasiddani9136 3 года назад

    nice explanation bro

  • @bhaveshvaishnav633
    @bhaveshvaishnav633 2 года назад

    Very well explained.

  • @jlohia8548
    @jlohia8548 4 года назад

    thank you

  • @adeoke3086
    @adeoke3086 4 года назад

    Good explanation. Very much appreciated.

  • @subeesha671
    @subeesha671 6 лет назад

    Keep rocking bro..fantastic explanation...

  • @santoshdanapur
    @santoshdanapur 3 года назад

    One question , in Call by value u used single number whereas in call by reference you used list. That part is not clear to me

    • @Whatever14253
      @Whatever14253 3 года назад

      look up mutable and immutbale objects. Immutable objects such as single numbers are always call by value in python. Mutable objects like lists are where pass by object reference comes to play

  • @PSR_Unfiltered
    @PSR_Unfiltered 4 года назад

    precise

  • @ngsbeats6308
    @ngsbeats6308 4 года назад

    in python some argument are passed by value and other by references depending their type ? true or false

  • @BigDoubleJoe
    @BigDoubleJoe 6 лет назад

    thanks

  • @moha_linus
    @moha_linus 5 лет назад

    Thanks sir ❤️

  • @parshvasoni1834
    @parshvasoni1834 5 лет назад +2

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

  • @bhavyar2986
    @bhavyar2986 5 лет назад

  • @praveenjp3617
    @praveenjp3617 5 лет назад

    So you are saying that python supports both call by value and reference?

    • @IndianPythonista
      @IndianPythonista  5 лет назад +1

      Don't think that way. Python actually supports something called call by object. Understand what that means as explained in the video.

    • @kushagra2255
      @kushagra2255 5 лет назад +1

      As list are mutable objects and integers & strings are immutable. So when you change the inc value of variable inside a function it actually makes a new container for it. But in case of list when you append something it does on same container. So calling that variable/list outside the function will give different result. In short python uses call-by-reference but if you pass immutable arguments like integers, strings or tuples to a function , the passing acts like call-by-value.

  • @SapnaSaini3
    @SapnaSaini3 5 лет назад +1

    really liked the way you explained it .Thanks !