Abstract Class
Abstract Class is a class that can't have instances but can have subclasses.
To create the abstract class, you need to import the ABC
(Abstract Base Class) from the abc
built-in library:
from abc import ABC
Also, you need to inherit from class ABC
:
class SomeClass(ABC):
pass
Now, we have the abstract class SomeClass
, but we can create instances:
12345678from abc import ABC class SomeClass(ABC): pass instance = SomeClass() instance.something = "Something" print(instance.something)
In the example above, we created an instance of SomeClass
because SomeClass
does not have any abstract methods.
To create an abstract class, it should follow the following structure:
- The abstract class should inherit from the
ABC
class. - The abstract class should define one or more abstract methods.
Note
A class is not considered abstract unless it has at least one abstract method and inherits from the
ABC
(Abstract Base Class) class.
We will describe abstract methods in the next chapter.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 2.78
Abstract Class
Sveip for å vise menyen
Abstract Class is a class that can't have instances but can have subclasses.
To create the abstract class, you need to import the ABC
(Abstract Base Class) from the abc
built-in library:
from abc import ABC
Also, you need to inherit from class ABC
:
class SomeClass(ABC):
pass
Now, we have the abstract class SomeClass
, but we can create instances:
12345678from abc import ABC class SomeClass(ABC): pass instance = SomeClass() instance.something = "Something" print(instance.something)
In the example above, we created an instance of SomeClass
because SomeClass
does not have any abstract methods.
To create an abstract class, it should follow the following structure:
- The abstract class should inherit from the
ABC
class. - The abstract class should define one or more abstract methods.
Note
A class is not considered abstract unless it has at least one abstract method and inherits from the
ABC
(Abstract Base Class) class.
We will describe abstract methods in the next chapter.
Takk for tilbakemeldingene dine!