Programmer's Python - Local and Global
Written by Mike James   
Monday, 28 January 2019
Article Index
Programmer's Python - Local and Global
Inner Functions

Without functions all we have are attributes of objects. Functions are where variables live. This extract is from my new book on Python.

Programmer's Python
Everything is an Object

Is now available as a print book: Amazon

pythoncover


Contents

  1. Hello Python World
  2. Variables, Objects and Attributes
  3. The Function Object
    Extract - Function Objects
  4. Scope, Lifetime and Closure
    Extract - Local and Global  ***NEW
  5. Advanced Functions
    Extract -  Decorators 
  6. Class Methods and Constructors
  7. Inside Class
  8. Metaclass
  9. Advanced Attributes
    Extract - Properties
  10. Custom Attribute Access
  11. Single Inheritance
  12. Multiple Inheritance
  13. Class and Type
  14. Type Annotation
  15. More Magic - Operator Overloading

 

Advanced Attributes

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.

For example:

myGlobalVariable=10
def myFunction():
    print(myGlobalVariable)
myFunction()

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.

For example:

myGlobalVariable=10
def myFunction():
    myGlobalVariable=0
    print(myGlobalVariable)
myFunction()
print(myGlobalVariable)

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.

For example:

myGlobalVariable=10
def myFunction():
    global myGlobalVariable
    myGlobalVariable=0
    print(myGlobalVariable)
myFunction()
print(myGlobalVariable)

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.

<ASIN:1871962587>



Last Updated ( Monday, 28 January 2019 )