|
| 1 | +''' |
| 2 | +Welcome to another python 3 basics video, in this video we're going to now |
| 3 | +discuss the concept of global and local variables. |
| 4 | +
|
| 5 | +When users begin using functions, they can quickly become confused when it comes |
| 6 | +to global and local variables... getting a the dreaded variable is not defined |
| 7 | +even when they clearly see that it is... or so they think. |
| 8 | +
|
| 9 | +These terms of global and local |
| 10 | +correspond to a variable's reach within a script or program. |
| 11 | +
|
| 12 | +A global variable is one that can be accessed anywhere |
| 13 | +
|
| 14 | +A local variable is the opposite, it can only be accessed within its frame. |
| 15 | +
|
| 16 | +The difference is that global variables can be accessed locally, but not modified |
| 17 | +locally inherently. |
| 18 | +
|
| 19 | +A local variable cannot be accessed globally, inherently. |
| 20 | +Now, dont worry about committing |
| 21 | +that to memory right now, I think it makes a lot more sense when you just |
| 22 | +see and do it, so let's do that. |
| 23 | +
|
| 24 | +''' |
| 25 | +# this variable has no parent function, but is actually NOT a global variable. |
| 26 | +# it just so happens that it is committed to memory before the function is called |
| 27 | +# so we are able to iterate, or call it out, but we cannot do much else. |
| 28 | + |
| 29 | +x = 6 |
| 30 | + |
| 31 | +def example(): |
| 32 | + # z, however, is a local variable. |
| 33 | + z = 5 |
| 34 | + # this works |
| 35 | + print(z) |
| 36 | + |
| 37 | +example() |
| 38 | +# this does not, which often confuses people, because z has been defined |
| 39 | +# and successfully even was called... the problem is that it is a local |
| 40 | +# variable only, and you are attempting to access it globally. |
| 41 | + |
| 42 | +print(z) |
| 43 | + |
| 44 | +# next up is an example that i've seen cause even more trouble, and that's |
| 45 | +# the attempt to play with a global variable locally. The reason why this |
| 46 | +# is so troubling is because you can access it... you just cannot play |
| 47 | +# with it, and this often frustrates people for a while. |
| 48 | + |
| 49 | + |
| 50 | +x = 6 |
| 51 | + |
| 52 | +def example2(): |
| 53 | + # works |
| 54 | + print(x) |
| 55 | + print(x+5) |
| 56 | + |
| 57 | + # but then what happens when we go to modify: |
| 58 | + x+=6 |
| 59 | + |
| 60 | + # so there we attempted to take the x var and add 6 to it... but now |
| 61 | + # we are told that we cannot, as we're referencing the variable before |
| 62 | + # its assignment. |
| 63 | + |
| 64 | +''' |
| 65 | +So now you know the rules, what can we do about it? |
| 66 | +''' |
| 67 | +x = 6 |
| 68 | + |
| 69 | +def example3(): |
| 70 | + # what we do here is defined x as a global variable. |
| 71 | + global x |
| 72 | + # now we can: |
| 73 | + print(x) |
| 74 | + x+=5 |
| 75 | + print(x) |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +''' |
| 81 | +So that is all for global and local, though I will show 1 last thing. |
| 82 | +
|
| 83 | +Sometimes you want a sort of global variable as a starting point, but |
| 84 | +you do not actually wish to modify the "global" variable outside of the |
| 85 | +functions themselves. You can just do the following: |
| 86 | +''' |
| 87 | + |
| 88 | +def example4(): |
| 89 | + globx = x |
| 90 | + # now we can: |
| 91 | + print(globx) |
| 92 | + globx+=5 |
| 93 | + print(globx) |
| 94 | + |
| 95 | + |
| 96 | +# and that's it! |
| 97 | + |
| 98 | +# hopefully that will help some of you from pulling your hair out for 30 minutes |
| 99 | +# trying to figure out what the heck is going on to reality. This is something |
| 100 | +# that snagged me pretty good when i was starting out. |
0 commit comments