|
| 1 | +############################### |
| 2 | +# Nested Statements and Scope # |
| 3 | +############################### |
| 4 | + |
| 5 | +# Now that we have gone over on writing our own functions, its important to |
| 6 | +# understand how Python deals with the variable names you assign. When you create |
| 7 | +# a variable name in Python the name is stored in a *name-space*. Variable names |
| 8 | +# also have a "scope", the scope determines the visibility of that variable name |
| 9 | +# to other parts of your code. |
| 10 | +# |
| 11 | +# Lets start with a quick thought experiment, imagine the following code: |
| 12 | + |
| 13 | +x = 25 |
| 14 | + |
| 15 | +def printer(): |
| 16 | + x = 50 |
| 17 | + return x |
| 18 | + |
| 19 | +print(x) |
| 20 | +print(printer()) |
| 21 | + |
| 22 | + |
| 23 | +# What do you imagine the output of printer() is? 25 or 50? What is the output |
| 24 | +# of print x? 25 or 50? Or what about this: |
| 25 | + |
| 26 | +print(x) |
| 27 | +print(printer()) |
| 28 | +print(x) |
| 29 | + |
| 30 | + |
| 31 | +# Interesting! But how does Python know which "x" you're referring to in your |
| 32 | +# code? This is where the idea of scope comes in. Python has a set of rules it |
| 33 | +# follows to decide what variables (such as x in this case) you are referencing |
| 34 | +# in your code. Lets break down the rules: |
| 35 | + |
| 36 | +# This idea of scope in your code is very important to understand in order to |
| 37 | +# properly assign and call variable names. |
| 38 | +# |
| 39 | +# In simple terms, the idea of scope can be described by 3 general rules: |
| 40 | +# |
| 41 | +# 1. Name assignments will create or change local names by default. |
| 42 | +# 2. Name references search (at most) four scopes, these are: |
| 43 | +# * local |
| 44 | +# * enclosing functions |
| 45 | +# * global |
| 46 | +# * built-in |
| 47 | +# 3. Names declared in global and nonlocal statements map assigned names to |
| 48 | +# enclosing module and function scopes. |
| 49 | +# |
| 50 | +# |
| 51 | +# The statement in #2 above can be defined by the LEGB rule. |
| 52 | +# |
| 53 | +# **LEGB Rule.** |
| 54 | +# |
| 55 | +# L: Local — Names assigned in any way within a function (def or lambda)), |
| 56 | +# and not declared global in that function. |
| 57 | +# |
| 58 | +# E: Enclosing function locals — Name in the local scope of any and all |
| 59 | +# enclosing functions (def or lambda), from inner to outer. |
| 60 | +# |
| 61 | +# G: Global (module) — Names assigned at the top-level of a module file, or |
| 62 | +# declared global in a def within the file. |
| 63 | +# |
| 64 | +# B: Built-in (Python) — Names preassigned in the built-in names module : |
| 65 | +# open,range,SyntaxError,... |
| 66 | + |
| 67 | +############################### |
| 68 | +### Quick examples of LEGB #### |
| 69 | +############################### |
| 70 | + |
| 71 | +# Local |
| 72 | + |
| 73 | +# x is local here: |
| 74 | +f = lambda x:x**2 |
| 75 | + |
| 76 | + |
| 77 | +# Enclosing function locals |
| 78 | +# |
| 79 | +# This occurs when we have a function inside a function (nested functions) |
| 80 | +# |
| 81 | + |
| 82 | +name = 'This is a global name' |
| 83 | + |
| 84 | +def greet(): |
| 85 | + # Enclosing function |
| 86 | + name = 'Sammy' |
| 87 | + |
| 88 | + def hello(): |
| 89 | + print('Hello '+name) |
| 90 | + |
| 91 | + hello() |
| 92 | + |
| 93 | +greet() |
| 94 | + |
| 95 | + |
| 96 | +# Note how Sammy was used, because the hello() function was enclosed inside of |
| 97 | +# the greet function! |
| 98 | + |
| 99 | +# Global |
| 100 | +# |
| 101 | +print name |
| 102 | + |
| 103 | + |
| 104 | +# Built-in |
| 105 | +# These are the built-in function names in Python (don't overwrite these!) |
| 106 | +# You will know if you've typed one based on its color! |
| 107 | + |
| 108 | +len |
| 109 | + |
| 110 | + |
| 111 | +# Local Variables |
| 112 | + |
| 113 | +# When you declare variables inside a function definition, they are not related |
| 114 | +# in any way to other variables with the same names used outside the function - |
| 115 | +# i.e. variable names are local to the function. This is called the scope of the |
| 116 | +# variable. All variables have the scope of the block they are declared in |
| 117 | +# starting from the point of definition of the name. |
| 118 | +# |
| 119 | +# Example: |
| 120 | + |
| 121 | +x = 50 |
| 122 | + |
| 123 | +def func(x): |
| 124 | + print('x is', x) |
| 125 | + x = 2 |
| 126 | + print('Changed local x to', x) |
| 127 | + |
| 128 | +func(x) |
| 129 | +print('x is still', x) |
| 130 | + |
| 131 | + |
| 132 | +# The first time that we print the value of the name x with the first line in |
| 133 | +# the function’s body, Python uses the value of the parameter declared in the |
| 134 | +# main block, above the function definition. |
| 135 | +# |
| 136 | +# Next, we assign the value 2 to x. The name x is local to our function. So, |
| 137 | +# when we change the value of x in the function, the x defined in the main block |
| 138 | +# remains unaffected. |
| 139 | +# |
| 140 | +# With the last print statement, we display the value of x as defined in the main |
| 141 | +# block, thereby confirming that it is actually unaffected by the local |
| 142 | +# assignment within the previously called function. |
| 143 | + |
| 144 | +################################ |
| 145 | +# The Global Statement |
| 146 | +################################ |
| 147 | + |
| 148 | +# If you want to assign a value to a name defined at the top level of the program |
| 149 | +# (i.e. not inside any kind of scope such as functions or classes), then you have |
| 150 | +# to tell Python that the name is not local, but it is global. We do this using |
| 151 | +# the global statement. It is impossible to assign a value to a variable defined |
| 152 | +# outside a function without the global statement. |
| 153 | +# |
| 154 | +# You can use the values of such variables defined outside the function |
| 155 | +# (assuming there is no variable with the same name within the function). |
| 156 | +# However, this is not encouraged and should be avoided since it becomes unclear |
| 157 | +# to the reader of the program as to where that variable’s definition is. Using |
| 158 | +# the global statement makes it amply clear that the variable is defined |
| 159 | +# in an outermost block. |
| 160 | +# |
| 161 | +# Example: |
| 162 | + |
| 163 | +x = 50 |
| 164 | + |
| 165 | +def func(): |
| 166 | + global x |
| 167 | + print('This function is now using the global x!') |
| 168 | + print('Because of global x is: ', x) |
| 169 | + x = 2 |
| 170 | + print('Ran func(), changed global x to', x) |
| 171 | + |
| 172 | +print('Before calling func(), x is: ', x) |
| 173 | +func() |
| 174 | +print('Value of x (outside of func()) is: ', x) |
| 175 | + |
| 176 | + |
| 177 | +# The global statement is used to declare that x is a global variable - hence, |
| 178 | +# when we assign a value to x inside the function, that change is reflected |
| 179 | +# when we use the value of x in the main block. |
| 180 | +# |
| 181 | +# You can specify more than one global variable using the same global statement |
| 182 | +# e.g. global x, y, z. |
| 183 | + |
| 184 | +############################### |
| 185 | +# Conclusion |
| 186 | +############################### |
| 187 | + |
| 188 | +# You should now have a good understanding of Scope (you may have already |
| 189 | +# intuitively felt right about Scope which is great!) One last mention is that |
| 190 | +# you can use the globals() and locals() functions to check what are your current |
| 191 | +# local and global variables. |
| 192 | +# |
| 193 | +# Another thing to keep in mind is that everything in Python is an object! I can |
| 194 | +# assign variables to functions just like I can with numbers! We will go over |
| 195 | +# this again in the decorator section of the course! |
0 commit comments