473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

What's wrong with this code snippet?

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

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Jan 4 '06 #1
9 1328

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

Jan 4 '06 #2
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
Jan 4 '06 #3
"Gerard Flanagan" <gr********@yahoo.co.uk> wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
Class methods must have at least the 'self' argument. So


Arrrrrgh! :)

Thanks guys!

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Jan 4 '06 #4
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
Lozovina <_karlo_@_mosor.net_> wrote:

[...]
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

--
Change is inevitable, progress is not.
Jan 5 '06 #5
Dave Hansen <id**@hotmail.com> wrote in
news:l8********************************@4ax.com:
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.

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Jan 5 '06 #6
On Thu, 5 Jan 2006 01:14:43 +0000 (UTC) in comp.lang.python, Karlo
Lozovina <_karlo_@_mosor.net_> wrote:
Dave Hansen <id**@hotmail.com> wrote in
news:l8********************************@4ax.com :
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.


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

--
Change is inevitable, progress is not.
Jan 5 '06 #7
Dave Hansen <id**@hotmail.com> wrote in
news:7s********************************@4ax.com:
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.

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Jan 5 '06 #8
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

Jan 7 '06 #9
LordLaraby <Lo*********@gmail.com> wrote:
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
Jan 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jwayne | last post by:
I'm trying to implement a navigation bar with multiple a href classes. The following example works exactly as I want it to, but for Internet Explorer only. Firefox does not indent each link. ...
7
by: Vic | last post by:
Dear All, I found this code snippet on this list (taken from a nice webpage of a courteous fellow), which I used to filter a form on a combo box. I wanted to repeat the same code to have an...
9
by: Eric Lilja | last post by:
Hello, I have two code snippets I want you to look at. My program compiles without warnings (warning level set to max, gcc 3.4.3) with either snippet but the latter one causes a segfault at...
4
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan",...
7
by: Jason Kid | last post by:
Hi, Please tell me what's wrong with the following code. private bool CheckIt( int iArg, bool fArg) { bool fIsGood = false;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.