Page 1 of 2
Without functions all we have are attributes of objects. Functions are where variables live. This extract is from my new book on Python.
Everything is an Object
Is now available as a print book: Amazon
- Get Ready For The Python Difference
- Variables, Objects and Attributes
- The Function Object
- Scope, Lifetime and Closure
- Advanced Functions
- Class, Methods and Constructors
Extract 1: Objects Become Classes ***NEW!
- Inside Class
- Meeting Metaclasses
- Advanced Attributes
- Custom Attribute Access
- Single Inheritance
- Multiple Inheritance
- Class and Type
- Type Annotation
- Operator Overloading
- Python In Visual Studio Code
Extracts from the first edition
Functions bring something new – local variables.
The two work together in a way that is cooperative but you do have to keep in mind their differences. There is also an important difference between Python functions and most other language’s functions. Python functions are objects that exist even when the function is not being executed. This has some interesting consequences for the code that the function object is associated with.
First we need to find out about function nesting and scope.
Global v Local
You can define a function anywhere in code.
The simplest case is when a function is defined at the top level in a module. In this case all of the variables defined in the module are part of its execution context and are accessible from within the function. This is usually described as the function having access to the global variables of the module.
If you try to access a variable that isn’t defined as a local variable within the function, the variable table for the entity that the function is defined in is searched. If the variable is found then it is used.
In this case the print causes the system to look for myGlobalVariable in the local table. When it isn’t found the table of the containing entity, the module in this case, is searched and when the variable is found the value 10 is returned.
So far, so much like most languages. Python, however, works differently when storing a new reference in a variable. As Python has no keyword like var or let to signal that a new variable is being defined, an assignment to a variable always creates that variable.
Notice that now there is an assignment to myGlobalVariable within the function. This has to create a new local variable of the same name and store 0 in it, which is what the function prints. The print after the function has completed displays 10 as the global variable is not affected by the local variable – which has now been destroyed because the function has ended.
In most languages assigning to a variable that didn’t exist locally but did exist globally would result in an assignment to the global variable. This cannot be how it works in Python because assignment creates variables.
So how do you assign to a global variable in Python?
The answer is that you have to declare the variable global.
In this case the global modifier declares myGlobalVariable to be the same variable as in the enclosing entity which means that the assignment doesn’t create a new local variable. The result is that the print in the function and the one after the function both display 0. The function has modified the global variable.
A subtle point is that if the variable doesn’t exist as a global then it will be created when it is assigned to.
That is, global allows you to create global variables from within functions.
You can declare a list of variables as global:
global var1, var2, var3 ...
and all of these variables will be treated as global. The global declaration has to occur in the code before any assignments to the variable which would create a local variable of the same name.
Python does global and local the other way round compared to most languages but in most cases you want to make use of local variables and accessing globals is the exception.