inheritance TypeError: __init__() takes from 1 to 2 positional arguments but 8 were given

inheritance TypeError: __init__() takes from 1 to 2 positional arguments but 8 were given

我正在尝试遵循 Python OOP 书中与继承相关的示例。但是,我遇到了一个错误。

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)

错误是:

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

由于 Friend 正在调用 super().__init__(**kwargs) 并继承自 AddressHolderContact,您需要那些 类 的初始化程序所需的所有参数通过 kwargs 提供(即作为关键字参数)。您可以通过在 Friend.__init__ 中打印 kwargs 并使用关键字传递参数来查看其工作原理:

class Friend(Contact, AddressHolder):
    def __init__(self, /, phone: str = "", **kwargs) -> None:
        
        # Added line.
        print(kwargs)
        
        super().__init__(**kwargs)
        self.phone = phone

# Passing phone as positional argument, all the rest as keyword arguments.
f1 = Friend("123-456-7890", name="Dusty", email="dusty@example.com", street="Elm Street", city="New York City", state="New York", code="1100")

print(f1)
{'name': 'Dusty', 'email': 'dusty@example.com', 'street': 'Elm Street', 'city': 'New York City', 'state': 'New York', 'code': '1100'}
Friend('Dusty', 'dusty@example.com')

kwargs 将收集您提供的所有未在 Friend.__init__() 的签名中指定的关键字参数。由于您没有关键字参数,因此所有关键字参数都以 kwargs 结尾。 super().__init__(**kwargs) 将调用 Contact.__init__(**kwargs)AddressHolder.__init__(**kwargs),提供创建实例所需的所有参数。

您将所有参数作为 位置 传递,即没有关键字。你的错误是告诉你这不符合 Friend.__init__() 的签名,它只允许两个位置参数(selfphone)。当您输入关键字时错误消失,因为所有这些参数都以 kwargs.

结尾