using 'global' across source files.

Daniel Klein danielk at aracnet.com
Tue Jan 2 10:17:05 EST 2001


On Tue, 02 Jan 2001 13:41:07 +0100, Bram Stolk <bram at sara.nl> wrote:

>Hello,
>
>
>Using the 'global' keyword, I can have my functions
>access global variables, like:
>
>  # try.py
>  val=100
>
>  def change_val() :
>    global val
>    val = 2*val
>
>  change_val()
>  print val
>
>However, this scheme falls apart if I split up this
>code in two files, one containing the global, and
>one containing the func.
>
>  # prog.py
>
>  from funcs import *
>
>  val = 100
>
>  change_val()
>  print val
>
>  # funcs.py
>
>  def change_val() :
>    global val
>    val = val * 2
>

One solution would be to remove the 'global' and pass 'val' as an
argument, ie,

def change_val(val):
	return val * 2


import funcs
val = 100
funcs.change_val(val)


Daniel Klein



More information about the Python-list mailing list