Python
docs.python.org › 3 › library › abc.html
abc — Abstract Base Classes
This module provides the infrastructure for defining abstract base classes (ABCs) in Python, as outlined in PEP 3119; see the PEP for why this was added to Python.
Python Course
python-course.eu › oop › the-abc-of-abstract-base-classes.php
20. The 'ABC' of Abstract Base Classes | OOP | python-course.eu
from abc import ABC, abstractmethod class AbstractClassExample(ABC): def __init__(self, value): self.value = value super().__init__() @abstractmethod def do_something(self): pass
Videos
01:47
Python's __init__ Method | 2MinutesPy - YouTube
Abstract Base Class - Python
07:02
Learn Python ABSTRACT CLASSES in 7 minutes! 👻 - YouTube
10:05
Python Interfaces and Abstract Base Class (ABC): A Must-Know for ...
05:11
Python abstract base class (ABC) example - YouTube
12:29
Abstract Class and Abstract Method in Python - YouTube
W3Schools
w3schools.com › python › ref_module_abc.asp
Python abc Module
Python Examples Python Compiler ... Python Certificate Python Training · ❮ Standard Library Modules · Define an abstract base class and implement it: from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def ...
Earthly
earthly.dev › blog › abstract-base-classes-python
Abstract Base Classes in Python - Earthly Blog
July 19, 2023 - Let’s consider the Shape ABC we defined in the interfaces section, we have different shapes such as circles, rectangles, and triangles. Each shape has an area and a perimeter, so any class that inherits from Shape must implement both the area and perimeter methods. For example, we can define a Square concrete class that will inherit from Shape and provide implementations for both methods. # shape.py class Square(Shape): def __init__(self, side_length): self.side_length = side_length def area(self): return self.side_length ** 2 def perimeter(self): return 4 * self.side_length
Geek Python
geekpython.in › abc-in-python
Python's ABC: Understanding the Basics of Abstract Base Classes
October 29, 2023 - Python doesn’t allow creating objects for abstract classes because there is no actual implementation to invoke rather they require subclasses for implementation. ... We got the error stating that we cannot instantiate the abstract class Details with abstract methods called getname and getrole. Just as the abc module allows us to define abstract methods using the @abstractmethod decorator, it also allows us to define abstract properties using the @abstractproperty decorator.
DEV Community
dev.to › sachingeek › getting-started-with-pythons-abc-4b95
Getting Started With Python's ABC - DEV Community
April 6, 2023 - Python is not a fully object-oriented programming language but it supports the features like abstract classes and abstraction. We cannot create abstract classes directly in Python, so Python provides a module called abc that provides the infrastructure for defining the base of Abstract Base Classes(ABC).
Machine Learning Plus
machinelearningplus.com › blog › python abcs- the complete guide to abstract base classes
Python ABCs- The Complete Guide to Abstract Base Classes
July 15, 2025 - pythonCopy · # Simple abstract base class for documents class Document(ABC): def __init__(self, title): self.title = title @abstractmethod def save(self): pass @abstractmethod def get_extension(self): pass · The @abstractmethod decorator marks methods that must be implemented by subclasses.
Reddit
reddit.com › r/learnpython › abstract class, how to create it properly?
r/learnpython on Reddit: abstract class, how to create it properly?
March 24, 2024 -
I thought I understood what abstract class means but my professor just commented that it wasn't a abstract class. What I did is essentially this:
first instruction: create an abstract base class with two int attributes then derived another class called Hero with a string attribute which stores the title "hero"
from abc import ABC
class Person(ABC):
def __init__(self, height, speed):
self.height = height
self.speed = speed
def walk(self):
//walk method
from person import Person
class Hero(Person):
def __init__(self, height, speed):
super().__init__(height, speed)
self.person_title = "Hero"
was this the right way to do it?
Top answer 1 of 4
4
create an abstract base class with two int attributes That's not really possible in python. An abstract class must have at least one method decorated with @abc.abstractmethod. You can't make an abstract class with just 2 attributes. You should ask your professor to clarify what they want you to do.
2 of 4
3
It depends what your professor thinks "abstract class" means. Originally, abstract classes couldn't have any attributes, so that's out of the window. Others might say abstract classes are just classes that can't be instantiated directly, if that's your professor definition then yours work. You might also say that anything that inherits from ABC is abstract In reality, the example doesn't make much sense because the whole point of abstract classes is to enforce some kind of structure to subtypes and the example doesn't do that, Person could be a normal class. For example from abc import ABC, abstractmethod class Person(ABC): @abstractmethod def speak(): ... from person import Person class Hero(Person): def __init__(self): self.person_title = "Hero" def speak(self): print("foo") Now this makes sense because you're enforcing that Hero implements a speak method, it's impossible to forget to do that
CodeFatherTech
codefather.tech › home › blog › create an abstract class in python: a step-by-step guide
Create an Abstract Class in Python: A Step-By-Step Guide
December 8, 2024 - Have you enjoyed this tutorial? We went through quite a lot! We have started by explaining the concept of abstract classes that represent a common interface to create classes that follow well-defined criteria. The name of the Python module we have used in all the examples is ABC (Abstract Base Classes).
Top answer 1 of 6
101
Making the __init__ an abstract method:
from abc import ABCMeta, abstractmethod
class A(object):
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self, n):
self.n = n
if __name__ == '__main__':
a = A(3)
helps:
TypeError: Can't instantiate abstract class A with abstract methods __init__
Python 3 version:
from abc import ABCMeta, abstractmethod
class A(object, metaclass=ABCMeta):
@abstractmethod
def __init__(self, n):
self.n = n
if __name__ == '__main__':
a = A(3)
Works as well:
TypeError: Can't instantiate abstract class A with abstract methods __init__
2 of 6
9
A not so elegant solution can be this:
class A(object):
def __init__(self, n):
if self.__class__ == A:
raise Exception('I am abstract!')
self.n = n
Usage
class B(A):
pass
a = A(1) # Will throw exception
b = B(1) # Works fine as expected.
Upgrad
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
September 12, 2024 - In Python, abstract base classes (ABCs) can also contain concrete methods—methods that are completely implemented in the abstract class itself. The presence of concrete methods in an abstract class allows for a blend of enforced structure and reusable code. ... class Animal(ABC): @abstractmethod def sound(self): pass # Concrete method def describe(self): return "This is an animal." # Create a subclass of Animal class Dog(Animal): def __init__(self, name): self._name = name # Implement the abstract method def sound(self): return "Bark" # Create another subclass of Animal class Cat(Animal): de
DataCamp
datacamp.com › tutorial › python-abstract-classes
Python Abstract Classes: A Comprehensive Guide with Examples | DataCamp
January 22, 2025 - The ABC class is a built-in Python feature that serves as a fundamental basis for developing abstract classes. You must inherit from ABC to define an abstract class. The class is abstract and cannot be instantiated directly, as indicated by this inheritance.
Python.org
discuss.python.org › typing
Enforcing __init__ signature when implementing it as an abstractmethod - Typing - Discussions on Python.org
December 29, 2024 - Hello. I noticed that Pyright doesn’t check the signatures of abstractmethod implementations when the abstractmethod is __init__(). Here’s an example: from abc import ABC, abstractmethod class AbstractA(ABC): @abstractmethod def __init__(self, x: int, y: int): pass @abstractmethod def do_something(self, z: int, u: int): pass class RealA(AbstractA): def __init__(self, x: int): ## No static type checker error self.x = x def do_something...
CodeConverter
codeconverter.com › articles › python-abstract-class-example
Python Abstract Classes: ABC Module Tutorial | CodeConverter Blog
February 12, 2026 - from abc import ABC, abstractmethod class AbstractClassExample(ABC): @property @abstractmethod def some_property(self): pass class ConcreteClass(AbstractClassExample): def __init__(self): self._some_property = "Hello, world!" @property def some_property(self): return self._some_property @some_property.setter def some_property(self, value): self._some_property = value obj = ConcreteClass() print(obj.some_property) # Output: Hello, world!
Python Tutorial
pythontutorial.net › home › python oop › python abstract classes
Python Abstract Class
March 31, 2025 - from abc import ABC, abstractmethod class AbstractClassName(ABC): @abstractmethod def abstract_method_name(self): pass Code language: Python (python)
Python Module of the Week
pymotw.com › 2 › abc
abc – Abstract Base Classes - Python Module of the Week
Since ABCWithConcreteImplementation is an abstract base class, it isn’t possible to instantiate it to use it directly. Subclasses must provide an override for retrieve_values(), and in this case the concrete class massages the data before returning it at all. $ python abc_concrete_method.py base class reading data subclass sorting data ['line one', 'line three', 'line two']
Tutorialspoint
tutorialspoint.com › python › python_abstract_base_classes.htm
Python - Abstract Base Classes
When a class inherits from an Abstract Base Class (ABC) it must implement all abstract methods. If it doesn't then Python will raise a TypeError. Here is the example of enforcing implementation of the Abstract Base Class in Python − · class ...