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 🕵
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?
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.
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.
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!
Casey_w cleared this up. Global vars can be read (in or out of a function call) but not modified.
@keithlohmeyer Don't look at closures whatever you do. It would really confuse the issue🙃🙃
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 🕵
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.
I thought that when a variable was defined above the function it was global by default. Nice video
They are global but not available to manipulate in the function code unless you mark them as global inside the function.
@@keithlohmeyer Looks like way down on the learning curve. LOL
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?
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.
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.
Thanks for the clarification. That makes sense.
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!
The reply from Casey_w clarifies what I was seeing. For any serious code I agree with your thoughts.