Using The global Statement
If you want to assign a value to a name defined at the top level of the program i.e. not inside any kind of scope such as functions or classes , then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement. You can use the values of such variables defined outside the function assuming there is no variable with the same name within the...
Python enTable of Contents
13. Object Oriented Programming 21. Appendix Revision History Source http www. swaroopch. com mediawiki index.php oldid 1343 Contributors Swaroop, Waterox888, 3 anonymous edits
The Solution
As the design of our program is now reasonably stable, we can write the code which is an implementation of our solution. 1. The files and directories to be backed up are specified in a list. source 'C My Documents', 'C Code' Notice we had to use double quotes inside the string for names with spaces in it. 2. The backup must be stored in a main backup directory target_dir 'E Backup' Remember to change this to what you will be 3. The files are backed up into a zip file. 4. The name of the zip...
Using nonlocal statement
We have seen how to access variables in the local and global scope above. There is another kind of scope called nonlocal scope which is in-between these two types of scopes. Nonlocal scopes are observed when you define functions inside functions. Since everything in Python is just executable code, you can define functions anywhere. Let s take an example usr bin python Filename func_nonlocal.py When we are inside func_inner, the x defined in the first line of func_outer is relatively neither in...