[Tutor] Why a global?

Carlos carloslara at web.de
Wed Dec 27 12:38:10 CET 2006


Can help me with this:

I am using a python module that someone else wrote 

This is an example of how it works

# Usage:
# import ga
# ga = ga.GA()
# ga.evolve()

# Function arguments (fun defaults are used otherwise):
# ga.GA(population_size, gene_size, crossover_rate, mutation_rate, list_of_alleles)
# ga.evolve(number_of_generations_to_process)
# ga.set_fitness(your_fitness_function)

# Interesting public variables (besides the ones you can modify using arguments)
# ga.debug = 1   (turns on lots of output)
# ga.halt  = X.X (stop if this fitness is reached)

# Note: crossover_rate is the chance two entities will reproduce
#       mutation_rate is the chance a single entity will have an
allele changed

My problem is that no matter were I put this, I have to declare ga a 
global. So I ended up with this:

def runGA(Pop, Gen, It):

    # Declare a Global. This is the only solution for now
    global ga

    # Set the arguments of the GA   
    ga = ga.GA(pop = 2, alleles = range(10), gene_size = 8)
    ga.debug = 1

Can you help me with this information? or what else is needed for you to 
help me solve this problem?


More information about the Tutor mailing list