Scopes and Namespaces

Andrei Dumitrescu
A free video tutorial from Andrei Dumitrescu
DevOps Engineer and Professional Trainer
4.6 instructor rating • 7 courses • 20,142 students

Learn more from the full course

Master Network Automation with Python for Network Engineers

Real-Life Hands-On Python and Ansible Automation: Netmiko, Paramiko, Napalm, Telnet, Ansible, Cisco, Arista, Linux etc

18:03:02 of on-demand video • Updated September 2020

  • You will MASTER all the Python 3 key concepts starting from Scratch. No prior Python or programming knowledge is required
  • Learn how to automate the configuration of networking devices with Python 3 in a Multivendor Environment
  • Learn network programmability with Python, GNS3 and Ansible
  • Learn and apply advanced Python concepts like Multiprocessing and Multithreading in Network Automation
  • See real-world examples of automation scripts with Python for Cisco IOS, Arista EOS or Linux
  • Understand how to use Telnet and SSH with Python for network automation
  • Learn how to use and improve Paramiko and Netmiko for automation of common administration tasks with Python
  • Automate and troubleshoot network configurations with Ansible
  • You will be able to use Ansible, manage Ansible Inventory files, Tasks, Modules for Linux and networking devices, Playbooks, Vaults and YAML Files
  • Use NAPALM Python library in a Multivendor Environment
  • Learn how to configure networking devices with Python using a Serial Console Connection
  • Improve the existing Python modules and create your own modules for network automation
  • You will ultimately gain real-life skills for a good Network of DevOps Engineer
  • You will learn in-depth general Python Programming
English [Auto] In this lecture we'll take a deep look at scopes and name species in python a namespace A6 a name implies is a space holding some names in by name sign modifying two variables functions or classes we define. We can imagine unnamed space is a container or even a table that contains event names we define. This way we can have the same name or a variable defined in different name spaces a name or a variable can exist only in a specific part of our code. The portion of code where the name exists is called scope of that name or a variable into the binding between the variable in the object or a value is stored in a namespace. Each scope has its own namespace. So scopes and name species go hand-in-hand. In Python there are three types of named species or scopes the built in namespace contains Python built in functions. We've used a lot of ability in functions till this point. Print Len sorted range. Max are all built in functions. They are available to us whenever we want to use them. The global order module namespace contains all variables functions and classes we define in our scripts. And then there is the local namespace. It contains only names defined inside our own functions Lexi. Examples here in this script I've used the built in namespace. In fact I've used three built in functions topple arrange and print and I've also created a global scoped variable called T1 the built in. Names are available across all files or modules or for a python application and the global names can be used anywhere inside our module including inside any function but not outside the current file where they have been defined. Now if I try print x I'll get the nearer a name error. Python will look for x in the global or module namespace. It doesn't find it and then it will look for it in the built in namespace where it cannot find it either and returns a runtime error. But if I do X equals then in die around the script one more time I won't get an error the script brings the value of X without their vector because the name X has been created in the global namespace. So X is a global variable we can use it anywhere inside this script but not in other scripts Lexi another example I'll define a function my func and inside the function I am painting the value of x. Now I am calling the function this script printed X inside the function call on them when calling the function the Python interpreter is looking for a local variable called X. a local variable means defined inside the function. If it doesn't find it in the VIX the case here it will search for x in the global scope there it finds the name and uses it to index value inside of the function. If there is no X variable defined the neither in the local namespace. So inside the function nor global nor in the built in namespace it will return an error we can't see how it returned a name error we can see from this example how scopes are nested the local scope is nested inside the global scope which is nested inside the built in scope if you reference a variable name inside the scope and Python does not find it in that scopes namespace it will look for it in an enclosing scopes namespace it searches up the chain of all enclosing scope name spaces licks your mother example inside the function I write X equals five when we create functions we can create the variable names inside those functions using assignments variables defined inside the function are not created until the function is called Every time the function is called a new scope is created in variables defined inside the function are assigned to that scope when the function finishes running the scope is gone to now. In this example inside the function a new local variable called X is created when calling the function it finds a variable called X in the functions local namespace and uses it that variable exists only inside the function or in the function local namespace outside the function there is another X variable that stores another value here either right print x outside the function and either on the script we can't see how X inside the function stores value 5 and X outside the function stores value then this is a local variable inside the function and outside the function I have another variable a global one another example is when we want to use or to import a global name into a function namespace for that we use the global keyword for that name we want to use before any other statement that refers to the global variable I'll create another global variable called a equals then and the new function my func one in the function has one parameter like say B and inside the function I have global a a plus equals B and I'll print the value of a inside the function now I am going to call my function. So my func 1 and I'll base a value is argument like say 7 after the function call. I want to paint the value of a so print a outside the function and I'll run the script in this example inside the function we are importing and using the global a variable and that means that the result of the operations done on a inside the function is available also outside the function we can see how a equals 17 inside of the function and a also equals 17 outside the function because a is a global variable so inside the function I am using the global variable in Python doesn't create a local 1 licks yet another example the f my func to end inside the function I write but index and X equals eight and now I'll call the function. So my func to when writing this creep I'll get an error unbound local error local variable X referenced before assignment. Why is that. Whenever Python sees an assignment operation inside a function it interprets that variable is local. And this happens at compile time. In our example Python says Okay here is an assignment. So I am going to create a local variable dicks in this case the local variable X that will be created mosques or shadows. The global variable X defined earlier when wanting to paint the value of X. It returns an error because the global variable is masked and the local variable hasn't been created yet. In fact we are using a variable before creating it. It's the same like writing print looks a C and then a C equals 6. Here I am using a variable called C before creating that variable and fixed an error is the conclusion. If there are variables that I referenced inside the function but I not assigned a value anywhere in the function that variables will not be local and Python we look for them in the enclosing scope at runtime vexed because there is no assignment inside the function and it will consider X a global variable not a local one. And there is such a variable in the global scope here. X equals 10 in the last example from this lecture. I'll show you how a name can be mistakenly shadowed or masked. I am going to write. Print Len off and here a string. ABC thought of it. I am going to define a new function called Len of X in the inside the len function I write. Print x. Now I'll call the line function and by placing a string and I'll run the script we see how the built in Len function has been shadowed in fact by some kind of start the len function defined here in the global namespace and the not the len function defined in the built in namespace when I called the len function it pointed out the string based in is argument and the not that the number of characters of that string. This is a common error so you should never use names that are already defined. What can I do now. I can delete the name that shadows the built in name. So tell Len in this case. I delete the len function that has been defined in the script. After that I can run the len function and it will return the correct value so it looks a print Len off and a random string. We can't see how it targeted the number of characters in this string. I have called the origin all the built in Len function. All right. Thanks for watching it and I'll see you in the next video.