Seite 1 von 1

Vergleiche / euqals Methode

Verfasst: Mittwoch 23. April 2008, 15:36
von maxip
Hi,

folgendes Codebeispiel:

Code: Alles auswählen

class automobil:
    def __init__(self, marke, modell):
        self.marke = marke
        self.modell = modell

autos = [automobil("opel", "corsa"), automobil("opel", "astra"), automobil("ford", "mondeo")]

corsa = automobil("opel", "corsa")

if corsa in autos:
    print "corsa in autos"
else:
    print "corsa not in autos"
Welche Methode muss implementiert werden, damit in korrekt funktioniert ?

Danke :)

ciao

Verfasst: Mittwoch 23. April 2008, 15:40
von BlackJack
`__eq__()` oder `__cmp__()`.

Klassennamen werden üblicherweise in "MixedCase" geschrieben, also `Automarke` in diesem Fall.

Verfasst: Mittwoch 23. April 2008, 15:46
von maxip
ja, ich bin auch die Java Namenskonvetionen gewoehnt, war aber nur ein test...

Was meinst du mit Oder ? muss ich beide implementieren oder reicht eine ? Oder besser noch: wo steht das in der Doku ? Ich finds grad net :(

Aber mal danke bis hier :)

ciao

Verfasst: Mittwoch 23. April 2008, 15:48
von maxip
OK, mit Python und den funktionen oben hab ichs gefunden

ciao