I am trying to follow an example related to inheritance from a Python OOP book. However, I am facing an error.
from __future__ import annotations
class ContactList(list["Contact"]):
def search(self, name: str) -> list["Contact"]:
matching_contacts: list["Contact"] = []
for contact in self:
if name in contact.name:
matching_contacts.append(contact)
return matching_contacts
class Contact:
all_contacts = ContactList()
def __init__(self, /, name: str = "", email: str = "", **kwargs) -> None:
super().__init__(**kwargs)
self.name = name
self.email = email
self.all_contacts.append(self)
def __repr__(self) -> str:
return f"{self.__class__.__name__}(" f"{self.name!r}, {self.email!r}" f")"
class AddressHolder:
def __init__(self, /, street: str = "", city: str = "", state: str = "", code: str = "", **kwargs) -> None:
super().__init__(**kwargs)
self.street = street
self.city = city
self.state = state
self.code = code
class Friend(Contact, AddressHolder):
def __init__(self, /, phone: str = "", **kwargs) -> None:
super().__init__(**kwargs)
self.phone = phone
f1 = Friend("Dusty", "dusty@example.com", "Elm Street", "New York City", "New York", "1100", "123-456-7890")
print(f1)
The error is:
Traceback (most recent call last):
File "test.py", line 41, in <module>
f1 = Friend("Dusty", "dusty@example.com", "Elm Street", "New York City", "New York", "1100", "123-456-7890")
TypeError: Friend.__init__() takes from 1 to 2 positional arguments but 8 were given