What's wrong with this code snippet?

K

Karlo Lozovina

Here is it:

---
class Human:
def __init__(self, eye_one, eye_two):
self.eye_one = eye_one
self.eye_two = eye_two

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 :(.

Any help greatly appriciated :).
 
G

Gerard Flanagan

Karlo said:
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
 
B

Brian van den Broek

Karlo Lozovina said unto the world upon 04/01/06 04:19 PM:
Here is it:

---
class Human:
def __init__(self, eye_one, eye_two):
self.eye_one = eye_one
self.eye_two = eye_two

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 :(.


You left out self in your GenerateRandomColour method definition.

It should be:

def GenerateRandomColour(self):
rn.seed()
colour = rn.choice(['C', 'P', 'Z'])
return colour

HTH,

Brian vdB
 
D

Dave Hansen

You've received good answers to your original question. Just a side
issue...

On Wed, 4 Jan 2006 22:19:27 +0000 (UTC) in comp.lang.python, Karlo

[...]
def GenerateRandomColour():
rn.seed()
colour = rn.choice(['C', 'P', 'Z'])
return colour

I'm not sure what rn is, but it looks like a standard library
random.Random object. If so, I don't think you want to seed your PRNG
each time you use it -- your numbers might be much less random that
way. If it is the standard library object, the PRNG is seeded when
the module is first imported, so you may not need to seed it at all.

Regards,
-=Dave
 
K

Karlo Lozovina

I'm not sure what rn is, but it looks like a standard library
random.Random object. If so, I don't think you want to seed your PRNG
each time you use it -- your numbers might be much less random that
way. If it is the standard library object, the PRNG is seeded when
the module is first imported, so you may not need to seed it at all.

Yes, it is random module. So, what is the preferred way of seeding random
number generator? Just once in a file? Or something else?

I just needed some "random" choices to be made, so I didn't dig much into
random module. Anyway, thanks for pointing this out, appreciated.
 
D

Dave Hansen

Yes, it is random module. So, what is the preferred way of seeding random
number generator? Just once in a file? Or something else?

I just needed some "random" choices to be made, so I didn't dig much into
random module. Anyway, thanks for pointing this out, appreciated.

From my reading of the library docs, when you import random the PRNG
is seeded (using the current time) for you automatically.

If you call seed without a parameter, it will again use the current
time to seed the generator. But the generator only needs one seed to
generate a random sequence. Re-seeding the generator starts it
proding a new, different sequence. The resulting sequence of numbers
is really the first number in several distinct sequences.

It may be that the sequence of first numbers is "random enough" for
your use. But the call to seed is useless at best, and damaging at
worst.

Unless you want to reproduce the same string of random numbers,
there's really no reason to seed the generator yourself. Consider:
rep = random.Random()
rep.seed(1234)
first = [rep.random() for x in range(10)]
second = [rep.random() for x in range(10)]
rep.seed(1234)
third = [rep.random() for x in range(10)]
print first == second False
print first == third True
fourth = [rep.random() for x in range(10)]
print second == fourth True

In your code, I would simply remove the rn.seed() call. Regards,
-=Dave
 
K

Karlo Lozovina

In your code, I would simply remove the rn.seed() call. Regards,

And that's what I'm gonna do :). The random part is not that important to
my application so I wont investigate into further detail... anyway, thank
you.
 
L

LordLaraby

For what it's worth, GenerateRandomColour does not access any instance
variables and therefore needn't even be a member of the class
Population. If you wanted it there for encapsulation purposes, consider
making it a staticmethod like so:

@staticmethod()
def GenerateRandomColour():
rn.seed()
colour = rn.choice(['C', 'P', 'Z'])
return colour

Just something to consider...

LL
 
A

Alex Martelli

LordLaraby said:
For what it's worth, GenerateRandomColour does not access any instance
variables and therefore needn't even be a member of the class
Population. If you wanted it there for encapsulation purposes, consider
making it a staticmethod like so:

@staticmethod()
def GenerateRandomColour():
rn.seed()
colour = rn.choice(['C', 'P', 'Z'])
return colour

No parentheses on the decorator line, just '@staticmethod'. Otherwise,
fine.


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top