Python - Scope of Parameters/Arguments

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ม.ค. 2025

ความคิดเห็น • 13

  • @keithlohmeyer
    @keithlohmeyer  20 วันที่ผ่านมา +1

    Casey_w cleared this up. Global vars can be read (in or out of a function call) but not modified.

  • @charlotteswift
    @charlotteswift 10 วันที่ผ่านมา +1

    @keithlohmeyer Don't look at closures whatever you do. It would really confuse the issue🙃🙃

  • @bigbogeyface
    @bigbogeyface 23 วันที่ผ่านมา +1

    That's absolutely fascinating 🧐
    Is this a property only of the print() function? 🤔
    I'm going to do some digging 🔨
    I'm sure there's more rabbit holes around 🕳 here 🐇 somewhere 🕵

    • @keithlohmeyer
      @keithlohmeyer  23 วันที่ผ่านมา +1

      No the reason I dug this hole was a person was freely using outside variable to pass to other functions. I had never noticed that before.

  • @jameslewellen150
    @jameslewellen150 23 วันที่ผ่านมา +1

    I thought that when a variable was defined above the function it was global by default. Nice video

    • @keithlohmeyer
      @keithlohmeyer  23 วันที่ผ่านมา

      They are global but not available to manipulate in the function code unless you mark them as global inside the function.

    • @jameslewellen150
      @jameslewellen150 23 วันที่ผ่านมา +1

      @@keithlohmeyer Looks like way down on the learning curve. LOL

  • @charlotteswift
    @charlotteswift 11 วันที่ผ่านมา

    I've only just watched this video.
    I thought that you had already understood this, Keith. 😁😁😁😁
    Do you remember that, light years ago, Paul put a 'for i .... ' for loop inside another 'for i .... ' for loop and it worked!! Terribly confusing and a really bad idea but it WAS an accident.
    The rule of thumb that you can only modify a variable inside a function if
    a) it is declared inside the function
    OR
    b) if it's declared as global outside the function
    doesn't apply if it's a 'more complex' variable for example
    array = [1,2,3,4]
    a = 10
    def change_a_and_array():
    a = 13
    array[2] = 21
    change_a_and_array()
    print(f'{a=} {array=}')
    will give you:-
    a=10 array=[1, 2, 21, 4]
    This is why one can put flags inside a class and do whatever you want with them wherever you want. I can remember that you were using this approach at one time.
    Does this make any sense at all?

    • @keithlohmeyer
      @keithlohmeyer  11 วันที่ผ่านมา

      Yes I was using class variables in place of globals. I just had the wrong thought on outside variable use in functions. I think I am on the right track now. Thanks.

  • @Casey_W
    @Casey_W 21 วันที่ผ่านมา +1

    This has nothing to do with "test" being a function parameter. Global variables can be *accessed* without the global modifier but they cannot be modified. For example:
    test = 3
    def b():
    x = test * 2
    print(x)
    works fine. The "nonlocal" keyword does essentially the same thing for variables declared in outer (but not global) scopes.

    • @keithlohmeyer
      @keithlohmeyer  21 วันที่ผ่านมา

      Thanks for the clarification. That makes sense.

  • @lorisrobots
    @lorisrobots 21 วันที่ผ่านมา +1

    New info for me as well! I would consider that "poor" programming to use that approach. This comes from my days of programming professionally. I think it should be absolutely clear what variables you are referring to and you should declare a variable global if you need it inside. Good to know how it works. Thanks for sharing!

    • @keithlohmeyer
      @keithlohmeyer  21 วันที่ผ่านมา +1

      The reply from Casey_w clarifies what I was seeing. For any serious code I agree with your thoughts.