title
Python Tutorial: Variable Scope - Understanding the LEGB rule and global/nonlocal statements

description
In this Python Tutorial, we will be going over variable scope in Python. Scope is important because we need to understand it in just about every program we write. It allows us to understand where our variables can be seen from within our program and also what values these variables hold. It also helps with debugging, because scope is a common problem when errors are thrown. Let's get started. The code from this video can be found at: https://github.com/CoreyMSchafer/code_snippets/tree/master/Scope ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python

detail
{'title': 'Python Tutorial: Variable Scope - Understanding the LEGB rule and global/nonlocal statements', 'heatmap': [{'end': 67.962, 'start': 34.294, 'weight': 0.714}, {'end': 366.186, 'start': 313.188, 'weight': 0.703}, {'end': 749.498, 'start': 726.365, 'weight': 0.739}, {'end': 1116.266, 'start': 1011.518, 'weight': 0.765}], 'summary': 'This python tutorial covers variable scope, legb rule, global/nonlocal statements, and built-in functions. it emphasizes the order of scoping rules and the importance of using local scope for code maintenance and understanding.', 'chapters': [{'end': 96.366, 'segs': [{'end': 67.962, 'src': 'heatmap', 'start': 13.556, 'weight': 0, 'content': [{'end': 19.799, 'text': 'And understanding this also helps a lot when a variable may not have the value that we expected and we need to debug our code.', 'start': 13.556, 'duration': 6.243}, {'end': 22.662, 'text': "Now, if you don't know what I mean by variable scope now,", 'start': 20.319, 'duration': 2.343}, {'end': 29.709, 'text': 'this is what determines where our variables can be accessed from within the program and what values those variables hold in different contexts.', 'start': 22.662, 'duration': 7.047}, {'end': 33.754, 'text': "So let's go ahead and look at some examples to get a better understanding of this.", 'start': 30.03, 'duration': 3.724}, {'end': 38.219, 'text': "So there's a common abbreviation for understanding the scoping rules within Python.", 'start': 34.294, 'duration': 3.925}, {'end': 41.321, 'text': 'And that abbreviation is LEGB.', 'start': 38.619, 'duration': 2.702}, {'end': 46.345, 'text': 'And this stands for local, enclosing, global, and built-in.', 'start': 41.822, 'duration': 4.523}, {'end': 52.129, 'text': 'Now the local is which are variables defined within a function.', 'start': 46.825, 'duration': 5.304}, {'end': 57.093, 'text': 'Now enclosing are variables in the local scope of enclosing functions.', 'start': 52.67, 'duration': 4.423}, {'end': 59.495, 'text': "And I'm going to go over examples of all these in just a second.", 'start': 57.133, 'duration': 2.362}, {'end': 67.962, 'text': 'And the global are variables defined at the top level of a module or explicitly declared global using the global keyword.', 'start': 60.115, 'duration': 7.847}], 'summary': 'Variable scope in python is determined by legb: local, enclosing, global, and built-in.', 'duration': 43.537, 'max_score': 13.556, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw13556.jpg'}, {'end': 105.454, 'src': 'embed', 'start': 79.371, 'weight': 2, 'content': [{'end': 86.397, 'text': 'So Python first checks the variables in the local scope, then the enclosing scope, then the global, and then lastly the built-ins.', 'start': 79.371, 'duration': 7.026}, {'end': 93.924, 'text': "Now we're going to look at all these in this video, but instead of going in this exact order, let's first start off with the global and local scope,", 'start': 86.897, 'duration': 7.027}, {'end': 96.366, 'text': 'because these are probably the most commonly confused.', 'start': 93.924, 'duration': 2.442}, {'end': 105.454, 'text': "So let's say that I have a Python module here, where I simply set a variable x equal to a string global x.", 'start': 96.706, 'duration': 8.748}], 'summary': 'Python checks local, enclosing, global, and built-in scopes. focus on global and local scopes.', 'duration': 26.083, 'max_score': 79.371, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw79371.jpg'}], 'start': 0.329, 'title': 'Python variable scope', 'summary': 'Explains the importance of understanding variable scope in python, delves into the legb abbreviation for scoping rules, and highlights the order in which python determines variable assignment through local, enclosing, global, and built-in scopes.', 'chapters': [{'end': 96.366, 'start': 0.329, 'title': 'Python variable scope', 'summary': 'Explains the importance of understanding variable scope in python, delves into the legb abbreviation for scoping rules, and highlights the order in which python determines variable assignment through local, enclosing, global, and built-in scopes.', 'duration': 96.037, 'highlights': ['The LEGB abbreviation stands for local, enclosing, global, and built-in, which determines the scoping rules within Python.', 'Understanding variable scope is crucial for debugging code and knowing where variables can be accessed and what values they hold in different contexts.', 'Python follows the order of local, enclosing, global, and built-in scopes when determining variable assignment.', 'The global and local scopes are the most commonly confused scopes in Python.']}], 'duration': 96.037, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw329.jpg', 'highlights': ['Understanding variable scope is crucial for debugging code and knowing where variables can be accessed and what values they hold in different contexts.', 'The LEGB abbreviation stands for local, enclosing, global, and built-in, which determines the scoping rules within Python.', 'Python follows the order of local, enclosing, global, and built-in scopes when determining variable assignment.', 'The global and local scopes are the most commonly confused scopes in Python.']}, {'end': 608.284, 'segs': [{'end': 173.249, 'src': 'embed', 'start': 149.091, 'weight': 0, 'content': [{'end': 159.32, 'text': "So Python used that LEGB rule when I printed out that Y variable and it said, okay, so first I'm going to check if I have Y in my local scope.", 'start': 149.091, 'duration': 10.229}, {'end': 165.004, 'text': 'which are variables that are defined within the function, and it found that local y variable and printed it out.', 'start': 159.7, 'duration': 5.304}, {'end': 173.249, 'text': "So now within this test function, I'm going to comment out that print y, and instead I'm going to also print x here.", 'start': 165.504, 'duration': 7.745}], 'summary': "Python's legb rule prioritizes local scope variables, as shown by printing local y and x within the test function.", 'duration': 24.158, 'max_score': 149.091, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw149091.jpg'}, {'end': 366.186, 'src': 'heatmap', 'start': 313.188, 'weight': 0.703, 'content': [{'end': 322.498, 'text': 'So this just creates a local x variable here that lives only within this function and anything outside of that local scope still sees the global x variable.', 'start': 313.188, 'duration': 9.31}, {'end': 330.661, 'text': 'Okay, but what if we actually wanted to set a new value for that global x variable from within this test function?', 'start': 322.998, 'duration': 7.663}, {'end': 337.565, 'text': 'Now, to do this, we can explicitly tell Python that the x variable we want to work with is the global x variable.', 'start': 331.102, 'duration': 6.463}, {'end': 341.846, 'text': 'And to do this at the top, we can just say global x.', 'start': 337.965, 'duration': 3.881}, {'end': 344.428, 'text': "And now it knows that we're working with that global x variable.", 'start': 341.846, 'duration': 2.582}, {'end': 349.45, 'text': 'So now if I rerun this, then you can see that it ran this test function.', 'start': 344.788, 'duration': 4.662}, {'end': 356.617, 'text': "came in and said that we're working with this global x variable, set that to local x, printed that out, which was local x,", 'start': 349.83, 'duration': 6.787}, {'end': 364.024, 'text': 'and since we explicitly told Python that we wanted to change that global x variable, when we printed it, even outside the function,', 'start': 356.617, 'duration': 7.407}, {'end': 366.186, 'text': 'it still printed out this local x string.', 'start': 364.024, 'duration': 2.162}], 'summary': "Python allows to work with global variables within functions. explicitly stating 'global x' enables modification of the global variable.", 'duration': 52.998, 'max_score': 313.188, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw313188.jpg'}, {'end': 465.557, 'src': 'embed', 'start': 445.405, 'weight': 1, 'content': [{'end': 455.35, 'text': 'So you can imagine how difficult it would be to maintain our code if you use these global statements and had to worry about variables in your functions overriding values outside of that function.', 'start': 445.405, 'duration': 9.945}, {'end': 462.355, 'text': "So with the local scope being self contained, it allows us to not have to worry about what's going on outside of our function.", 'start': 455.71, 'duration': 6.645}, {'end': 465.557, 'text': 'So I wanted to show you how the global statement works.', 'start': 462.695, 'duration': 2.862}], 'summary': 'Local scope prevents variable conflicts, improving code maintenance.', 'duration': 20.152, 'max_score': 445.405, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw445405.jpg'}, {'end': 519.539, 'src': 'embed', 'start': 488.834, 'weight': 2, 'content': [{'end': 491.997, 'text': "So it's not going to exist outside of that function either.", 'start': 488.834, 'duration': 3.163}, {'end': 496.901, 'text': "It's just like when we set our local x variable within this function.", 'start': 492.477, 'duration': 4.424}, {'end': 504.407, 'text': 'The only difference is that as a function parameter, it can now be assigned values that are passed into our function.', 'start': 497.301, 'duration': 7.106}, {'end': 508.951, 'text': 'So, for example, when I call my test function down here now,', 'start': 505.027, 'duration': 3.924}, {'end': 519.539, 'text': "I'm going to pass in local z this string and that's going to set that local variable z equal to this local z string.", 'start': 508.951, 'duration': 10.588}], 'summary': 'Function parameters allow assignment of passed values, e.g. local z string.', 'duration': 30.705, 'max_score': 488.834, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw488834.jpg'}], 'start': 96.706, 'title': 'Understanding python scopes and variables', 'summary': 'Explains the concepts of local, enclosing, and global scope in python, along with the global statement and function parameters, emphasizing the importance of using local scope for better code maintenance and understanding.', 'chapters': [{'end': 173.249, 'start': 96.706, 'title': 'Legb rule in python - understanding scope', 'summary': 'Explains the legb rule in python, where it discusses the concept of global and local variables, demonstrating how python uses the legb rule to determine variable scope in functions, with an example of printing local and global variables within a function.', 'duration': 76.543, 'highlights': ['Python uses the LEGB rule to determine the scope of variables, which stands for Local, Enclosing, Global, and Built-in.', 'Demonstrates the concept of global and local variables, with variable x being a global variable and variable y being a local variable within the function test.', 'Explains how Python utilizes the LEGB rule to prioritize variable scope, with the example of printing local and global variables within the test function.']}, {'end': 608.284, 'start': 173.389, 'title': 'Understanding python scopes and variables', 'summary': 'Explains the concepts of local, enclosing, and global scope in python, along with the global statement and function parameters, emphasizing the importance of using local scope for better code maintenance and understanding.', 'duration': 434.895, 'highlights': ['Python uses local, enclosing, and global scope to resolve variable names, prioritizing the global scope if a variable is not found in the local or enclosing scope. Python resolves variable names using local, enclosing, and global scopes, prioritizing the global scope if a variable is not found in the local or enclosing scope.', 'The global statement can be used to explicitly modify global variables within a function, ensuring the changes reflect outside the function as well. The global statement can explicitly modify global variables within a function, reflecting the changes outside the function as well.', 'Function parameters allow the assignment of values passed into the function, creating local variables that exist only within the function. Function parameters enable the assignment of values passed into the function, creating local variables that exist only within the function.', 'Emphasizes the importance of using local scope for better code maintenance and understanding, discouraging overuse of the global statement. Emphasizes the importance of using local scope for better code maintenance and understanding, discouraging overuse of the global statement.']}], 'duration': 511.578, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw96706.jpg', 'highlights': ['Python uses the LEGB rule to determine the scope of variables, which stands for Local, Enclosing, Global, and Built-in.', 'The global statement can be used to explicitly modify global variables within a function, ensuring the changes reflect outside the function as well.', 'Function parameters allow the assignment of values passed into the function, creating local variables that exist only within the function.', 'Emphasizes the importance of using local scope for better code maintenance and understanding, discouraging overuse of the global statement.']}, {'end': 757.563, 'segs': [{'end': 638.095, 'src': 'embed', 'start': 608.724, 'weight': 0, 'content': [{'end': 614.047, 'text': "Now we were able to use min here because it's a built-in function in Python.", 'start': 608.724, 'duration': 5.323}, {'end': 623.871, 'text': "So if we'd like to view the variables that are within the built-in scope, then we can simply say import built-ins.", 'start': 614.447, 'duration': 9.424}, {'end': 634.074, 'text': 'And then after we import that, then I can just print out DIR on that built-ins module.', 'start': 624.591, 'duration': 9.483}, {'end': 638.095, 'text': 'Now DIR just gets a list of the attributes of a given object.', 'start': 634.354, 'duration': 3.741}], 'summary': "Using python's built-in function 'min' and 'dir' to view variables and attributes.", 'duration': 29.371, 'max_score': 608.724, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw608724.jpg'}, {'end': 694.749, 'src': 'embed', 'start': 667.13, 'weight': 2, 'content': [{'end': 672.934, 'text': 'Now, one thing that you want to be careful with as far as built-ins go is accidentally overriding them.', 'start': 667.13, 'duration': 5.804}, {'end': 676.316, 'text': "So this isn't something that Python prevents us from doing.", 'start': 673.274, 'duration': 3.042}, {'end': 682.06, 'text': "Now there's reasons for this, but basically they're just trusting us with having the power to do that.", 'start': 676.656, 'duration': 5.404}, {'end': 690.946, 'text': 'So for example, if I was to create a min function here, so let me comment out that printed built-ins there.', 'start': 682.42, 'duration': 8.526}, {'end': 694.749, 'text': 'So if I was to create a min function, function here.', 'start': 691.306, 'duration': 3.443}], 'summary': 'Be careful with overriding built-in functions in python.', 'duration': 27.619, 'max_score': 667.13, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw667130.jpg'}, {'end': 757.563, 'src': 'heatmap', 'start': 726.365, 'weight': 1, 'content': [{'end': 731.147, 'text': 'Now. the reason for that is when we ran this min function here.', 'start': 726.365, 'duration': 4.782}, {'end': 737.03, 'text': 'Python found our min function in the global scope before it fell back to the built-in scope.', 'start': 731.147, 'duration': 5.883}, {'end': 738.511, 'text': 'So you have to be careful with that.', 'start': 737.29, 'duration': 1.221}, {'end': 749.498, 'text': 'So if I change the name of my global min function here to something like my min and rerun that code, then now you can see that it works again,', 'start': 739.051, 'duration': 10.447}, {'end': 757.563, 'text': "because now it doesn't find min in that global scope and instead uses the built-in min function just like we expected.", 'start': 749.498, 'duration': 8.065}], 'summary': 'Changing global min function to my min allows built-in min function to be used.', 'duration': 26.416, 'max_score': 726.365, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw726365.jpg'}], 'start': 608.724, 'title': 'Using built-in functions in python', 'summary': 'Discusses the usage of built-in functions in python, such as importing the built-ins module, caution about accidental overriding, and the significance of scoping for conflict avoidance.', 'chapters': [{'end': 757.563, 'start': 608.724, 'title': 'Using built-in functions in python', 'summary': 'Discusses using built-in functions in python, including importing the built-ins module to view the attributes, caution about accidentally overriding built-ins, and the importance of scoping in avoiding conflicts when using built-in functions.', 'duration': 148.839, 'highlights': ['Importing the built-ins module to view the attributes The chapter explains how to import the built-ins module to view the attributes of the built-in scope in Python.', 'Caution about accidentally overriding built-ins The chapter emphasizes the importance of being careful when creating custom functions to avoid accidentally overriding built-in functions in Python, illustrated with an example of overriding the min function.', 'Importance of scoping in avoiding conflicts when using built-in functions The chapter underscores the significance of scoping in Python to avoid conflicts when using built-in functions, demonstrated through an example of resolving conflicts by modifying the global function name.']}], 'duration': 148.839, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw608724.jpg', 'highlights': ['Importing the built-ins module to view the attributes', 'Importance of scoping in avoiding conflicts when using built-in functions', 'Caution about accidentally overriding built-ins']}, {'end': 1011.037, 'segs': [{'end': 800.752, 'src': 'embed', 'start': 778.497, 'weight': 0, 'content': [{'end': 786.402, 'text': "Now, the reason that we saved the enclosing scope for last is because it's easier to understand once we understand the local and global scope.", 'start': 778.497, 'duration': 7.905}, {'end': 790.625, 'text': 'So the enclosing scope has to do with nested functions.', 'start': 787.143, 'duration': 3.482}, {'end': 794.948, 'text': "So let's say, for example, that I have a function within a function.", 'start': 790.985, 'duration': 3.963}, {'end': 800.752, 'text': "So I'm just going to say I'm going to make a function called outer here.", 'start': 795.048, 'duration': 5.704}], 'summary': 'Enclosing scope explained in relation to local and global scope.', 'duration': 22.255, 'max_score': 778.497, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw778497.jpg'}, {'end': 887.914, 'src': 'embed', 'start': 854.474, 'weight': 2, 'content': [{'end': 857.655, 'text': 'So we have our outer function here that we ran.', 'start': 854.474, 'duration': 3.181}, {'end': 861.777, 'text': 'So we came in here and we set our local x variable.', 'start': 858.176, 'duration': 3.601}, {'end': 865.118, 'text': 'Now this variable is local to our outer function.', 'start': 862.157, 'duration': 2.961}, {'end': 877.566, 'text': 'and now we come in here and run our inner function and within our inner function we set this x variable and this x variable is local to our inner function,', 'start': 865.718, 'duration': 11.848}, {'end': 880.208, 'text': 'and then we print that x variable.', 'start': 877.566, 'duration': 2.642}, {'end': 887.914, 'text': 'now it uses our rule legb, so it looked if it had any local x variables, and it does.', 'start': 880.208, 'duration': 7.706}], 'summary': 'Nested functions utilize local x variables according to legb rule.', 'duration': 33.44, 'max_score': 854.474, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw854474.jpg'}, {'end': 936.841, 'src': 'embed', 'start': 907.481, 'weight': 4, 'content': [{'end': 913.025, 'text': "So now let's comment out where we set our x variable within our inner function.", 'start': 907.481, 'duration': 5.544}, {'end': 919.549, 'text': 'So now, if I rerun this, then you can see that it printed out outer x both times.', 'start': 913.425, 'duration': 6.124}, {'end': 923.072, 'text': 'And this is what the enclosing scope is.', 'start': 920.07, 'duration': 3.002}, {'end': 928.636, 'text': 'So when we got to the point within our inner function where we printed out this x,', 'start': 923.472, 'duration': 5.164}, {'end': 936.841, 'text': "it first checked if it had any x variables local to that inner function, and it doesn't, because we just commented it out.", 'start': 929.176, 'duration': 7.665}], 'summary': 'Demonstrating the concept of enclosing scope within inner functions.', 'duration': 29.36, 'max_score': 907.481, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw907481.jpg'}, {'end': 1011.037, 'src': 'embed', 'start': 969.957, 'weight': 5, 'content': [{'end': 975.619, 'text': "When we set x in our inner function, it doesn't affect the x variable in the outer function.", 'start': 969.957, 'duration': 5.662}, {'end': 979.181, 'text': 'And just like with our global and local scopes,', 'start': 976.139, 'duration': 3.042}, {'end': 987.206, 'text': 'if I come out here and comment out this outer x and then uncomment out that inner x and save that and run that,', 'start': 979.181, 'duration': 8.025}, {'end': 996.091, 'text': 'then we get an error because this printed out x within our inner function, because it has that local x variable within the inner function.', 'start': 987.206, 'duration': 8.885}, {'end': 1002.414, 'text': "And then, when we try to print it outside of that function, It checked its local scope, but that's commented out.", 'start': 996.111, 'duration': 6.303}, {'end': 1009.437, 'text': "and then its enclosing scope, which it doesn't have any enclosing functions, and then global, which there's no global X function, and then built-ins.", 'start': 1002.414, 'duration': 7.023}, {'end': 1011.037, 'text': 'So it threw an error there.', 'start': 1009.737, 'duration': 1.3}], 'summary': 'Local x variable causes error due to scope conflict.', 'duration': 41.08, 'max_score': 969.957, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw969957.jpg'}], 'start': 758.103, 'title': "Python's enclosing scope", 'summary': 'Explains the enclosing scope in python, emphasizing its relevance within nested functions, illustrating the legb rule, and demonstrating practical examples of variable access and modification within nested functions.', 'chapters': [{'end': 853.954, 'start': 758.103, 'title': 'Understanding enclosing scope in python', 'summary': 'Explains the enclosing scope in python, emphasizing its relevance within nested functions and its relationship with local and global scopes, while demonstrating a practical example of defining and accessing variables within nested functions.', 'duration': 95.851, 'highlights': ['The enclosing scope in Python is related to nested functions and is crucial to comprehend after understanding local and global scopes.', 'The demonstration of defining and accessing variables within nested functions provides a practical understanding of the enclosing scope concept.', 'The practical example illustrates the usage of nested functions and accessing variables within these functions.']}, {'end': 928.636, 'start': 854.474, 'title': 'Scope in nested functions', 'summary': 'Explains the concept of variable scope within nested functions, illustrating the local enclosing global built-in (legb) rule and demonstrating the impact of enclosing scope on variable access.', 'duration': 74.162, 'highlights': ['The concept of variable scope within nested functions is explained, demonstrating the Local Enclosing Global Built-in (LEGB) rule and its impact on variable access.', 'The variable x is set within the outer function, and a separate x variable is set within the inner function, both being local to their respective functions.', 'The impact of the enclosing scope is illustrated by commenting out the setting of the x variable within the inner function, resulting in the printing of the outer x variable both times when the code is rerun.']}, {'end': 1011.037, 'start': 929.176, 'title': 'Enclosing scopes in functions', 'summary': 'Explains how enclosing scopes work in functions, demonstrating how the local scope of enclosing functions affects variable access and modification within inner functions, and how it differs from global and local scopes, with a specific example of encountering an error when accessing an outer function variable from an inner function.', 'duration': 81.861, 'highlights': ['Demonstrates the concept of enclosing scopes in functions and the impact on variable access and modification within inner functions. N/A', 'Illustrates the difference between the local scope of enclosing functions and the global scope, showing how setting a variable in an inner function does not affect the variable in the outer function. N/A', 'Provides a specific example of encountering an error when attempting to access an outer function variable from an inner function due to the absence of the variable in the enclosing scope. N/A']}], 'duration': 252.934, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw758103.jpg', 'highlights': ['The demonstration of defining and accessing variables within nested functions provides a practical understanding of the enclosing scope concept.', 'The concept of variable scope within nested functions is explained, demonstrating the Local Enclosing Global Built-in (LEGB) rule and its impact on variable access.', 'The variable x is set within the outer function, and a separate x variable is set within the inner function, both being local to their respective functions.', 'The practical example illustrates the usage of nested functions and accessing variables within these functions.', 'The impact of the enclosing scope is illustrated by commenting out the setting of the x variable within the inner function, resulting in the printing of the outer x variable both times when the code is rerun.', 'Illustrates the difference between the local scope of enclosing functions and the global scope, showing how setting a variable in an inner function does not affect the variable in the outer function.', 'Provides a specific example of encountering an error when attempting to access an outer function variable from an inner function due to the absence of the variable in the enclosing scope.']}, {'end': 1247.914, 'segs': [{'end': 1062.683, 'src': 'embed', 'start': 1031.309, 'weight': 0, 'content': [{'end': 1036.894, 'text': "So you may be wondering if there's a way that you can do this with the enclosing scope variables as well.", 'start': 1031.309, 'duration': 5.585}, {'end': 1038.375, 'text': 'And there is a way to do that.', 'start': 1037.354, 'duration': 1.021}, {'end': 1046.621, 'text': "So let's say within our inner function here, that we wanted to actually change the x variable of our outer function.", 'start': 1038.795, 'duration': 7.826}, {'end': 1051.666, 'text': "Now we shouldn't use global because global will affect the global scope.", 'start': 1047.122, 'duration': 4.544}, {'end': 1055.45, 'text': "In this case, we'd use the non local statement.", 'start': 1052.067, 'duration': 3.383}, {'end': 1062.683, 'text': "So up at the top here, I'm going to atop of this inner function, I'm going to say non local x and save that.", 'start': 1055.83, 'duration': 6.853}], 'summary': 'Inner functions can modify outer scope variables using nonlocal statement.', 'duration': 31.374, 'max_score': 1031.309, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw1031309.jpg'}, {'end': 1130.254, 'src': 'heatmap', 'start': 1011.518, 'weight': 1, 'content': [{'end': 1019.841, 'text': "Okay, so now let's go ahead and set that back to how it was, where we comment out that inner X and uncomment out that outer X and rerun that.", 'start': 1011.518, 'duration': 8.323}, {'end': 1023.403, 'text': 'Okay, so when we looked at our global and local variables,', 'start': 1020.501, 'duration': 2.902}, {'end': 1030.969, 'text': 'I showed you how you could use that global statement in order to explicitly tell Python that you wanted to work with the global x variable.', 'start': 1023.403, 'duration': 7.566}, {'end': 1036.894, 'text': "So you may be wondering if there's a way that you can do this with the enclosing scope variables as well.", 'start': 1031.309, 'duration': 5.585}, {'end': 1038.375, 'text': 'And there is a way to do that.', 'start': 1037.354, 'duration': 1.021}, {'end': 1046.621, 'text': "So let's say within our inner function here, that we wanted to actually change the x variable of our outer function.", 'start': 1038.795, 'duration': 7.826}, {'end': 1051.666, 'text': "Now we shouldn't use global because global will affect the global scope.", 'start': 1047.122, 'duration': 4.544}, {'end': 1055.45, 'text': "In this case, we'd use the non local statement.", 'start': 1052.067, 'duration': 3.383}, {'end': 1062.683, 'text': "So up at the top here, I'm going to atop of this inner function, I'm going to say non local x and save that.", 'start': 1055.83, 'duration': 6.853}, {'end': 1067.526, 'text': 'Now this will allow us to work with the local variables of enclosing functions.', 'start': 1063.163, 'duration': 4.363}, {'end': 1073.13, 'text': "So in this case, it means that we're now affecting this x variable of the outer function.", 'start': 1067.866, 'duration': 5.264}, {'end': 1081.656, 'text': 'So now if I uncommon out that inner x, and I rerun this, and now you can see that it prints out inner x twice.', 'start': 1073.891, 'duration': 7.765}, {'end': 1093.367, 'text': "And that's because when we set our x here to inner x, within our inner function, we were actually affecting that local x of our enclosing function.", 'start': 1082.016, 'duration': 11.351}, {'end': 1096.852, 'text': 'So it prints out inner x within this inner function.', 'start': 1093.648, 'duration': 3.204}, {'end': 1100.937, 'text': 'And also when we print it out here, it got overwritten here.', 'start': 1097.412, 'duration': 3.525}, {'end': 1103.359, 'text': 'So now it prints out inner x twice.', 'start': 1101.037, 'duration': 2.322}, {'end': 1110.003, 'text': 'Now I can actually see the non-local statement being used more often than the global statement,', 'start': 1103.9, 'duration': 6.103}, {'end': 1116.266, 'text': 'because non-local can be useful in order to change the state of closures and decorators and things like that.', 'start': 1110.003, 'duration': 6.263}, {'end': 1119.748, 'text': "Now I'm not going to go into closures and decorators in this video,", 'start': 1116.607, 'duration': 3.141}, {'end': 1125.371, 'text': "but if you're interested then I do have a video on both of those topics and if you watch those,", 'start': 1119.748, 'duration': 5.623}, {'end': 1130.254, 'text': 'then you could probably think of some use cases where using this non-local statement could be useful.', 'start': 1125.371, 'duration': 4.883}], 'summary': "Non-local statement used to affect enclosing function's local variables; useful for closures, decorators.", 'duration': 48.238, 'max_score': 1011.518, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw1011518.jpg'}, {'end': 1187.395, 'src': 'embed', 'start': 1158.536, 'weight': 3, 'content': [{'end': 1161.737, 'text': "So, now that we've gone over the LEGB rule,", 'start': 1158.536, 'duration': 3.201}, {'end': 1171.702, 'text': 'then you should know exactly what would happen if I was to come in here and comment out where I set the x here within this inner function.', 'start': 1161.737, 'duration': 9.965}, {'end': 1181.47, 'text': "So if I run that, then you can see that when we printed that, it didn't find it in its local scope and instead fell back to that enclosing scope.", 'start': 1172.022, 'duration': 9.448}, {'end': 1187.395, 'text': 'And if I was to comment out where I set the x here within this outer function and ran that,', 'start': 1181.79, 'duration': 5.605}], 'summary': 'Discussed legb rule and demonstrated scope resolution in python functions.', 'duration': 28.859, 'max_score': 1158.536, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw1158536.jpg'}, {'end': 1228.068, 'src': 'embed', 'start': 1202.9, 'weight': 5, 'content': [{'end': 1208.762, 'text': "And if it didn't find anything in that global scope, then it would look in that list of built-ins that we looked at earlier.", 'start': 1202.9, 'duration': 5.862}, {'end': 1211.763, 'text': "And if it doesn't find it there, then an error is thrown.", 'start': 1209.162, 'duration': 2.601}, {'end': 1220.326, 'text': 'And also after this video, you should know how to use the global and non-local statements to change the values within those scopes if need be.', 'start': 1212.343, 'duration': 7.983}, {'end': 1222.867, 'text': "So I think that's going to do it for this video.", 'start': 1221.186, 'duration': 1.681}, {'end': 1228.068, 'text': "If you have any questions about what we covered, then feel free to ask in the comment section below and I'll do my best to answer those.", 'start': 1223.207, 'duration': 4.861}], 'summary': 'Learn to use global and non-local statements for scope manipulation in python.', 'duration': 25.168, 'max_score': 1202.9, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw1202900.jpg'}], 'start': 1011.518, 'title': 'Python scopes and statements', 'summary': 'Explains non-local statement in python for changing the state of enclosing function variables and demonstrates the legb rule for variable scopes, highlighting practical applications and variable behavior within different scopes.', 'chapters': [{'end': 1130.254, 'start': 1011.518, 'title': 'Understanding non-local statement in python', 'summary': 'Explains the usage of non-local statement in python to change the state of enclosing function variables, showcasing how it can be used to affect the local variable of the enclosing function and its practical applications, emphasizing its usefulness over the global statement.', 'duration': 118.736, 'highlights': ['The non-local statement allows for the manipulation of the local variables of enclosing functions, as demonstrated by changing the x variable of the outer function within the inner function. By using the non-local statement, the speaker showcases the ability to modify the local variable of the enclosing function, providing a practical example of affecting the x variable of the outer function within the inner function.', "The impact of the non-local statement is illustrated through the printing of 'inner x' twice, signifying the alteration of the local x of the enclosing function. The consequence of using the non-local statement is exemplified by the output of 'inner x' being printed twice, highlighting the modification of the local x of the enclosing function within the inner function.", 'The speaker emphasizes the frequent use of the non-local statement compared to the global statement, citing its usefulness in changing the state of closures and decorators. The speaker underscores the prevalence of the non-local statement over the global statement, emphasizing its practicality in modifying the state of closures and decorators, indicating its relevance in various programming scenarios.']}, {'end': 1247.914, 'start': 1130.754, 'title': 'Understanding legb rule in scopes', 'summary': 'Covers the legb rule in python, demonstrating the behavior of variable scopes and the use of global and non-local statements, providing insights into how variables are accessed within different scopes and the fallback to global and built-in scopes as necessary.', 'duration': 117.16, 'highlights': ['The chapter covers the LEGB rule in Python Key point: Explains the LEGB rule in Python for variable scoping.', 'Demonstrating the behavior of variable scopes and the use of global and non-local statements Key point: Illustrates the behavior of variable scopes and the use of global and non-local statements for modifying variable values.', 'Providing insights into how variables are accessed within different scopes and the fallback to global and built-in scopes as necessary Key point: Explains how variables are accessed within different scopes and the fallback to global and built-in scopes when necessary.']}], 'duration': 236.396, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/QVdf0LgmICw/pics/QVdf0LgmICw1011518.jpg', 'highlights': ['The non-local statement allows for the manipulation of the local variables of enclosing functions, as demonstrated by changing the x variable of the outer function within the inner function.', "The impact of the non-local statement is illustrated through the printing of 'inner x' twice, signifying the alteration of the local x of the enclosing function.", 'The speaker emphasizes the frequent use of the non-local statement compared to the global statement, citing its usefulness in changing the state of closures and decorators.', 'The chapter covers the LEGB rule in Python Key point: Explains the LEGB rule in Python for variable scoping.', 'Demonstrating the behavior of variable scopes and the use of global and non-local statements Key point: Illustrates the behavior of variable scopes and the use of global and non-local statements for modifying variable values.', 'Providing insights into how variables are accessed within different scopes and the fallback to global and built-in scopes as necessary Key point: Explains how variables are accessed within different scopes and the fallback to global and built-in scopes when necessary.']}], 'highlights': ['Understanding variable scope is crucial for debugging code and knowing where variables can be accessed and what values they hold in different contexts.', 'The LEGB abbreviation stands for local, enclosing, global, and built-in, which determines the scoping rules within Python.', 'Python follows the order of local, enclosing, global, and built-in scopes when determining variable assignment.', 'The global and local scopes are the most commonly confused scopes in Python.', 'The global statement can be used to explicitly modify global variables within a function, ensuring the changes reflect outside the function as well.', 'Function parameters allow the assignment of values passed into the function, creating local variables that exist only within the function.', 'Emphasizes the importance of using local scope for better code maintenance and understanding, discouraging overuse of the global statement.', 'Importing the built-ins module to view the attributes', 'Importance of scoping in avoiding conflicts when using built-in functions', 'Caution about accidentally overriding built-ins', 'The demonstration of defining and accessing variables within nested functions provides a practical understanding of the enclosing scope concept.', 'The concept of variable scope within nested functions is explained, demonstrating the Local Enclosing Global Built-in (LEGB) rule and its impact on variable access.', 'The variable x is set within the outer function, and a separate x variable is set within the inner function, both being local to their respective functions.', 'The practical example illustrates the usage of nested functions and accessing variables within these functions.', 'The impact of the enclosing scope is illustrated by commenting out the setting of the x variable within the inner function, resulting in the printing of the outer x variable both times when the code is rerun.', 'Illustrates the difference between the local scope of enclosing functions and the global scope, showing how setting a variable in an inner function does not affect the variable in the outer function.', 'Provides a specific example of encountering an error when attempting to access an outer function variable from an inner function due to the absence of the variable in the enclosing scope.', 'The non-local statement allows for the manipulation of the local variables of enclosing functions, as demonstrated by changing the x variable of the outer function within the inner function.', "The impact of the non-local statement is illustrated through the printing of 'inner x' twice, signifying the alteration of the local x of the enclosing function.", 'The speaker emphasizes the frequent use of the non-local statement compared to the global statement, citing its usefulness in changing the state of closures and decorators.', 'The chapter covers the LEGB rule in Python Key point: Explains the LEGB rule in Python for variable scoping.', 'Demonstrating the behavior of variable scopes and the use of global and non-local statements Key point: Illustrates the behavior of variable scopes and the use of global and non-local statements for modifying variable values.', 'Providing insights into how variables are accessed within different scopes and the fallback to global and built-in scopes as necessary Key point: Explains how variables are accessed within different scopes and the fallback to global and built-in scopes when necessary.']}