Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Abstract Method | Polymorphism and Abstraction
In-Depth Python OOP

bookAbstract Method

Abstract Method is a method that should be redefined in subclasses.

To create an abstract method, you should import the @abstractmethod decorator from the abc library.

from abc import ABC, abstractmethod

Note

Pay attention: decorator is imported without the @ symbol.

12345678
from abc import ABC, abstractmethod class SomeClass(ABC): @abstractmethod def some_method(): pass instance = SomeClass() # TypeError
copy

The abstract methods should be implemented in subclasses. You can't create a subclass without redefining all abstract methods:

12345678910111213141516
from abc import ABC, abstractmethod class AbstractParent(ABC): @abstractmethod def first_method(): pass @abstractmethod def second_method(): pass class Child(AbstractParent): def first_method(self): print("The first method") instance = Child() # TypeError
copy

The class Child saved the Abstract Class state because the second_method is not redefined, and the ABC class exists in the inheritance hierarchy.

12345678910111213141516171819202122
from abc import ABC, abstractmethod class AbstractParent(ABC): @abstractmethod def first_method(): pass @abstractmethod def second_method(): pass class Child(AbstractParent): def first_method(self): print("The first method") def second_method(self): print("The second method") instance = Child() instance.first_method() instance.second_method()
copy

Note

The abstract classes and methods exist to provide a strict class structure.

question mark

Abstract Class must have:

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 6

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 2.78

bookAbstract Method

Свайпніть щоб показати меню

Abstract Method is a method that should be redefined in subclasses.

To create an abstract method, you should import the @abstractmethod decorator from the abc library.

from abc import ABC, abstractmethod

Note

Pay attention: decorator is imported without the @ symbol.

12345678
from abc import ABC, abstractmethod class SomeClass(ABC): @abstractmethod def some_method(): pass instance = SomeClass() # TypeError
copy

The abstract methods should be implemented in subclasses. You can't create a subclass without redefining all abstract methods:

12345678910111213141516
from abc import ABC, abstractmethod class AbstractParent(ABC): @abstractmethod def first_method(): pass @abstractmethod def second_method(): pass class Child(AbstractParent): def first_method(self): print("The first method") instance = Child() # TypeError
copy

The class Child saved the Abstract Class state because the second_method is not redefined, and the ABC class exists in the inheritance hierarchy.

12345678910111213141516171819202122
from abc import ABC, abstractmethod class AbstractParent(ABC): @abstractmethod def first_method(): pass @abstractmethod def second_method(): pass class Child(AbstractParent): def first_method(self): print("The first method") def second_method(self): print("The second method") instance = Child() instance.first_method() instance.second_method()
copy

Note

The abstract classes and methods exist to provide a strict class structure.

question mark

Abstract Class must have:

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 6
some-alt