Local and Global Variable Scope

Sdílet
Vložit
  • čas přidán 28. 08. 2024

Komentáře • 13

  • @stevew9945
    @stevew9945 Před 3 lety +1

    Really helped me, thanks

  • @2forview770
    @2forview770 Před 7 lety +3

    very very good explanation .... thanks a lot .

  • @kuatroka
    @kuatroka Před 8 lety +1

    Thanks for the explanation, John.

  • @poudelroshan
    @poudelroshan Před 5 lety +1

    Great Explanation! Thank you

  • @MaxGoddur
    @MaxGoddur Před 7 lety

    Outstanding, well done!

  • @cerberes
    @cerberes Před 9 lety

    Hey John, I ran an ID() on the variables and found the global variable ID changed to the local variable ID after the product was returned. I think I understand it correctly because its now pointing to the local variables value of 6.. Am i correct?
    Great series also.. I just finished your series and enjoyed it very much.. its very well produced and everything is explained thoroughly with great examples.

    • @johnphilipjones
      @johnphilipjones  Před 9 lety +2

      Ron Hunn Hello Ron, That is correct it is because when local variables is returned it returns the 'object pointer' to this local variable (everything in Python is an object). So the id (pointer) of the variable in the function is returned to the variable in the 'global space' this overwrites whatever id the global variable had i.e. the global variable now points to the variable that was created in the function. However, the name of the variable used in the function (i.e. the local variable) no loner exists so it does no longer points to the object it originally created. Passing and returning with respect to functions and there calls always passes and returns the 'object pointer' (which is represented by the id).
      Best wishes
      Phil

  • @iebah1
    @iebah1 Před 7 lety

    It looks like the function find_product has changed the value of the global variable product. Is this correct? I've been trying to learn python 3.6 and I didn't think functions could change the value of a global variable. Is this different in python 2.x or have I misunderstood? Thanks, Tom

    • @johnphilipjones
      @johnphilipjones  Před 7 lety

      www.pythonbytesize.com/video-python-parameter-passing-mechanism.html
      I recommend you view this video

    • @iebah1
      @iebah1 Před 7 lety

      Wow that was quick! I think I know now how I got confused. Thanks.

  • @divyagairola8499
    @divyagairola8499 Před 7 lety

    hey nice explanation but i am stuck with this example:
    p=0
    def prod(num1,num2):
    p=num1*num2
    print ("inside func ",p)
    return p
    print("global",p)
    prod(5,6)
    print("global",p)
    when i run this peice of code i get different values:
    global 0
    inside func 30
    global 0

    • @johnphilipjones
      @johnphilipjones  Před 7 lety +1

      The variable p within the function only exists during the execution of the function i.e. its lifetime is that of the execution of the function. It also has a local scope and is only available to the code within the the function. The function is returning the value stored within the local p but your code has not assigned this return to the global p i.e. you have used prod(5,6) it should be:
      p =prod(5,6)
      Regards
      Phil

    • @divyagairola8499
      @divyagairola8499 Před 7 lety +1

      John Philip Jones thanks a lot got it..