473,770 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_COUN T):
self.house.appe nd(Human(self.G enerateRandomCo lour(),
self.GenerateRa ndomColour()))

def GenerateRandomC olour():
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.appe nd(Human(self.G enerateRandomCo lour(),
self.GenerateRa ndomColour()))
TypeError: GenerateRandomC olour() takes no arguments (1 given)
---

If I remove GenerateRandomC olour 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 1343

Karlo Lozovina wrote:
Here is it:
class Population:
def __init__(self):
self.house = []
for i in range(0, POPULATION_COUN T):
self.house.appe nd(Human(self.G enerateRandomCo lour(),
self.GenerateRa ndomColour()))

def GenerateRandomC olour():
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.appe nd(Human(self.G enerateRandomCo lour(),
self.GenerateRa ndomColour()))
TypeError: GenerateRandomC olour() takes no arguments (1 given)
---

If I remove GenerateRandomC olour 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 GenerateRandomC olour(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_COUN T):
self.house.appe nd(Human(self.G enerateRandomCo lour(),
self.GenerateRa ndomColour()))

def GenerateRandomC olour():
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.appe nd(Human(self.G enerateRandomCo lour(),
self.GenerateRa ndomColour()))
TypeError: GenerateRandomC olour() takes no arguments (1 given)
---

If I remove GenerateRandomC olour 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 GenerateRandomC olour method definition.

It should be:

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

HTH,

Brian vdB
Jan 4 '06 #3
"Gerard Flanagan" <gr********@yah oo.co.uk> wrote in
news:11******** **************@ g49g2000cwa.goo glegroups.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.pytho n, Karlo
Lozovina <_karlo_@_mosor .net_> wrote:

[...]
def GenerateRandomC olour():
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.c om> wrote in
news:l8******** *************** *********@4ax.c om:
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.pytho n, Karlo
Lozovina <_karlo_@_mosor .net_> wrote:
Dave Hansen <id**@hotmail.c om> 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.c om> wrote in
news:7s******** *************** *********@4ax.c om:
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, GenerateRandomC olour 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 GenerateRandomC olour():
rn.seed()
colour = rn.choice(['C', 'P', 'Z'])
return colour

Just something to consider...

LL

Jan 7 '06 #9
LordLaraby <Lo*********@gm ail.com> wrote:
For what it's worth, GenerateRandomC olour 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 GenerateRandomC olour():
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
3755
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. http://jonathanwayne.com/test/test.html Here's the relevant snippet: <div class="nav"> <a href="#">link 0</a>
7
3369
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 additional combo control doing a different filtering on the same form, and simply copied the code below and changed the appropriate combo names. ______________________________________________________ Private Sub ExpByType_AfterUpdate() If...
9
1380
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 run-time. I know it contains non-standard constructs (using the MySql C api) but I wanted to know if it's printf() I'm misuing. Here are the snippets: /* m is of type MYSQL* */ unsigned int code = mysql_errno(m); const char *description =...
4
4119
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", PRO_REQUIRED }; const char* TOS_network_spantree_set_0_para_1_emvalue = { "disable", "enable", NULL }; PARA TOS_network_spantree_set_0_para_1 = { "", emENUM, TOS_network_spantree_set_0_para_1_emvalue, "", "enable or disable STP", PRO_REQUIRED };
7
1805
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
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.