1. Nauka
  2. /
  3. Kursy
  4. /
  5. Programowanie obiektowe w Pythonie

Connected

ćwiczenie

Porównywanie a dziedziczenie

Co się dzieje, gdy obiekt jest porównywany z obiektem klasy podrzędnej? Rozważ następujące dwie klasy:

class Parent:
    def __eq__(self, other):
        print("Parent's __eq__() called")
        return True

class Child(Parent):
    def __eq__(self, other):
        print("Child's __eq__() called")
        return True

Klasa Child dziedziczy po klasie Parent, a obie implementują metodę __eq__() z komunikatem diagnostycznym.

Instrukcje 1/1

undefined XP
    1

Pytanie

Która metoda __eq__() zostanie wywołana po uruchomieniu poniższego kodu?

p = Parent()
c = Child()

p == c 

Możesz swobodnie eksperymentować w konsoli – klasy są już dla ciebie zdefiniowane.

Możliwe odpowiedzi