[Tutor] how to get str() to use my function?
knacktus at googlemail.com
Wed Aug 4 17:55:47 CEST 2010
Am 04.08.2010 17:37, schrieb Alex Hall:
> It worked, thanks. Is there a list of these functions somewhere? That
> is, the functions that map implicitly to operators or implied uses?
> For example, printing will call __str__, as will a cal to str(). What
> about math or comparison operators? I have heard of __eq__, __gt__,
> and so on, but I tried to implement one and I got an error saying that
> it required three arguments. It did, but only because the first was
> self. I put the function inside my card class:
> def __eq__(self, card1, card2):
> #end def __eq__
> For some reason it is still looking for three arguments...
The official list of all special methods can be found at the datamodel
description, chapter 3.4:
Most Python books also have some explanations about this. Maybe the nice
online book "Dive into Python" has a chapter? (I didn't check...)
Regarding your problem: I've flew very fast over the docs. __eq__ seems
to expect two arguments (self, other). So probably your method of the
card class should look like:
def __eq__(self, card2):
More information about the Tutor