Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: Python error: TypeError: setrating() takes exactly 2 arguments (3 given)  (gelezen 1231 keer)

Offline bart85

  • Lid
Onderstaande code zit een error in:
def setrating(name, R):
x = 0
while x < len(self.members):
if self.members[x].name == name:
self.members[x].R = R
return
x += 1

Citaat
>>> a.setrating('Bart', 1200)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: setrating() takes exactly 2 arguments (3 given)
« Laatst bewerkt op: 2014/02/28, 17:26:32 door bartveurink »
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Re: Python error: TypeError: setrating() takes exactly 2 arguments (3 given)
« Reactie #1 Gepost op: 2014/02/28, 18:19:44 »
Ik denk dat je  self bent vergeten in de definitie

def setrating(self, name, R):

Offline bart85

  • Lid
Re: Python error: TypeError: setrating() takes exactly 2 arguments (3 given)
« Reactie #2 Gepost op: 2014/02/28, 18:27:49 »
Klopt, Waarvoor dient self?
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Re: Python error: TypeError: setrating() takes exactly 2 arguments (3 given)
« Reactie #3 Gepost op: 2014/02/28, 19:19:53 »
Klopt, Waarvoor dient self?

Bij twijfel, print gewoon de variabele:

print self # Python 2
print(self) # Python 3

Elke class method heeft automatisch een eerste parameter, self, die verwijst naar de instantie van de class waartoe deze method behoort.

In uw voorbeeld gebruikt ge een paar keer self.members. De "self" variabele moet ergens vandaan komen en natuurlijk ook ergens naar verwijzen, anders kan hij de members attribuut niet vinden.