How to create a global variable in python ?

How to create a global variable in python ?

Daidalos April 29, 2020


Example of how to create a global variable in python:

1 -- Using global

To declare for example a as a global variable:

global a

2 -- Illustration with a function

Using global variables in a function:

 >>> global a,b
 >>> a = 0.5
 >>> b = 2
 >>> def function( x ):
...     return a * x + b
... 
 >>> function(1)
 2.5
 >>> b = 4
 >>> function(1)
 4.5

3 -- References