What's wrong with this code snippet?

Gerard Flanagan grflanagan at yahoo.co.uk
Wed Jan 4 17:40:39 EST 2006


Karlo Lozovina wrote:

> Here is it:

>
> class Population:
>     def __init__(self):
>         self.house = []
>         for i in range(0, POPULATION_COUNT):
>             self.house.append(Human(self.GenerateRandomColour(),
>                                     self.GenerateRandomColour()))
>
>     def GenerateRandomColour():
>         rn.seed()
>         colour = rn.choice(['C', 'P', 'Z'])
>         return colour
> ---
>
> Uppon running it gives this error:
>
> ---
> Initializing first generation population:
> Traceback (most recent call last):
>   File "population.py", line 38, in ?
>     earth = Population()
>   File "population.py", line 26, in __init__
>     self.house.append(Human(self.GenerateRandomColour(),
> self.GenerateRandomColour()))
> TypeError: GenerateRandomColour() takes no arguments (1 given)
> ---
>
> If I remove GenerateRandomColour from class definition, and put it as a
> separate function, everything works fine. I've been staring at this code
> for half an hour and can't find what's wrong :(.
>

Class methods must have at least the 'self' argument. So

    def GenerateRandomColour(self):
            *do something

Gerard




More information about the Python-list mailing list