Newbie: Classes and Lists

Sean 'Shaleh' Perry shalehperry at attbi.com
Tue Apr 2 22:41:10 EST 2002


> 
> import random
> 
> class Chromosome:
> 
>     data = []
>     

this makes data global to ALL members of Chromosome.  You want to move it into
the constructor.

>     def __init__(self, length):

ie. self.data = []

>         for bit in range(length):
>             self.data.append(random.randrange(0,2,1))
> 
>     def display(self):
>         print self.data
> 
> 
> population = []
> 
> for i in range(100):
>     x = Chromosome(20)
>     population.append(x)
> 
> population[50].display()
> 




More information about the Python-list mailing list