Python Ошибка "<method> отсутствует 1 обязательный позиционный аргумент: 'self'" - PullRequest
1 голос
/ 28 января 2020

Впервые до Python. Попытка создать простой пример, демонстрирующий абстракцию на 2 уровня. Получение ошибки TypeError: «Объект HPNotebook» не может быть вызван »

Я просмотрел множество примеров и все еще в замешательстве.

Чтобы понять, я показал 3 уровня в коде.
Можете ли вы указать мне место, которое поможет объяснить эту проблему и как ее устранить, или предложить предложение, как ее исправить. Спасибо

from abc import abstractmethod,ABC   #this is to allow abstraction. the ABC forces inherited classes to implement the abstracted methods.

class TouchScreenLaptop(ABC):
    def __init__(self):
        pass
    @abstractmethod      #indicates the following method is an abstract method.
    def scroll(self):    # a function within the parent
        pass             #specifically indicates this is not being defined
    @abstractmethod      #indicates the following method is an abstract method.
    def click(self):    
        pass             #specifically indicates this is not being defined

class HP(TouchScreenLaptop):
    def __init__(self):
        pass
    @abstractmethod         #indicates the following method is an abstract method.
    def click(self):    
        pass  
    def scroll(self):
        print("HP Scroll")

class HPNotebook(HP):
    def __init__(self):
        self()
    def click(self):
        print("HP Click")    
    def scroll(self):
        HP.scroll()

hp1=HPNotebook()
hp1.click()                  #the 2 level deep inherited function called by this instance
hp1.scroll()                 #the 1 level deep inherited function called by this instance

1 Ответ

2 голосов
/ 28 января 2020

Просто замените self() на super() на HPNotebook.__init__ и замените HP.scroll() на super().scroll() на HPNotebook.scroll.

class HPNotebook(HP):
    def __init__(self):
        super()
    def click(self):
        print("HP Click")    
    def scroll(self):
        super().scroll()

Также отметьте эту ссылку для лучшего понимания о python наследовании.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...