why don't my global variables get initialized?

Nathan Froyd froydnj at cs.rose-hulman.edu
Tue Feb 15 12:05:50 EST 2000


I'm writing a python program with some global variables that I need
initialized. For testing purposes, I thought I would write a function that
would do the Right Thing and read in everything correctly:

def init_global_data(filename):
	global num_cities, citylist, matrix
	zfile = open(filename, 'r')

	num_cities = read_number_of_cities(zfile)
	citylist = read_city_names(zfile)
	matrix = read_distance_matrix(zfile, num_cities)

However, this doesn't seem to be working, because none of those three global
variables are initialized after I call the function:

Python 1.5.2 (#1, Jul 22 1999, 11:59:40)  [GCC 2.8.1] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from genetics import *
>>> init_global_data('att48.tsp')
>>> num_cities
0
>>> citylist
[]
>>> matrix
[]

Those are the default values those variables started out with.

What am I missing? I know you can change global variables and I thought the
way I was doing it was the proper way, but it's obviously not. What's wrong?
Thanks!




More information about the Python-list mailing list