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__
Answer from Mike Müller on Stack Overflow
🌐
Python
docs.python.org › 3 › library › abc.html
abc — Abstract Base Classes
The ABC MyIterable defines the standard iterable method, __iter__(), as an abstract method. The implementation given here can still be called from subclasses. The get_iterator() method is also part of the MyIterable abstract base class, but ...
🌐
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)
🌐
W3Schools
w3schools.com › python › ref_module_abc.asp
Python abc Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass class Square(Shape): def __init__(self, s): self.s = s def area(self): return self.s * self.s sq = Square(3) print(isinstance(sq, Shape)) print(sq.area()) Try it Yourself »
🌐
GeeksforGeeks
geeksforgeeks.org › python › abstract-classes-in-python
Abstract Classes in Python - GeeksforGeeks
Explanation: make_sound() is an abstract method in the Animal class, so it doesn't have any code inside it. If you try to instantiate Animal directly, Python will raise a TypeError since the method is not implemented.
Published   September 3, 2025
🌐
DataCamp
datacamp.com › tutorial › python-abstract-classes
Python Abstract Classes: A Comprehensive Guide with Examples | DataCamp
January 22, 2025 - Python reports a TypeError when a subclass is attempted to be instantiated if it does not override all abstract methods. This stringent enforcement lowers the likelihood of defects and improves code stability by assisting developers in identifying implementation flaws early. It also guarantees that all concrete subclasses follow the abstract class's intended behavior and design. ... class Rectangle(Shape): def __init__(self, width, height): self.width = width self.height = height # Missing area and perimeter implementation # This will raise an error rectangle = Rectangle(5, 10) # TypeError: Can't instantiate abstract class Rectangle with abstract methods area, perimeter
🌐
Python Course
python-course.eu › oop › the-abc-of-abstract-base-classes.php
20. The 'ABC' of Abstract Base Classes | OOP | python-course.eu
Abstract classes cannot be instantiated, and require subclasses to provide implementations for the abstract methods.
🌐
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 - $ python aircraft.py Traceback (most recent call last): File "aircraft.py", line 3, in <module> class Aircraft(ABC): File "aircraft.py", line 10, in Aircraft @property File "/Users/codefathertech/opt/anaconda3/lib/python3.7/abc.py", line 23, in abstractmethod funcobj.__isabstractmethod__ = True AttributeError: attribute '__isabstractmethod__' of 'property' objects is not writable · Let’s also override the constructor in the Jet class: class Jet(Aircraft): def __init__(self, speed): self.__speed = speed def fly(self): print("My jet is flying")
🌐
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?

Find elsewhere
🌐
DEV Community
dev.to › sarahs › abstract-classes-in-python-55mj
Abstract Classes in Python - DEV Community
December 20, 2023 - I've been working a lot in C# for school lately, but I was previously learning Python over the summer, so I will do this challenge in Python3 as a refresher. ... Title:, a space, and then the current instance's title. Author:, a space, and then the current instance's author. Price:, a space, and then the current instance's price. ... from abc import ABCMeta, abstractmethod class Book(object, metaclass=ABCMeta): def __init__(self,title,author): self.title=title self.author=author @abstractmethod def display(): pass #Write MyBook class title=input() author=input() price=int(input()) new_novel=MyBook(title,author,price) new_novel.display()
🌐
w3resource
w3resource.com › python › python-abstract-classes-and-interfaces.php
Understanding Python Abstraction: Abstract Classes & Interfaces
... The 'turn_on' and 'turn_off' ... implemented and used in practice. ... Abstraction In Python is a way to define a common interface for a group of related classes....
🌐
MakeUseOf
makeuseof.com › home › programming › abstract classes in python: a beginner's guide
Abstract Classes in Python: A Beginner's Guide
September 11, 2021 - At this point, it would be good to mention that—unlike Java—abstract methods in Python can have an implementation.. This implementation can be accessed in the overriding method using the super() method. import abc class AbstractClass(ABC): def __init__(self, value): self.value = value super().__init__() @abc.abstractmethod def some_action(self): print("This is the parent implementation.") class MySubclass(AbstractClassExample): def some_action(self): super().some_action() print("This is the subclass implementation.
🌐
Medium
medium.com › @mhesty71 › what-i-wish-i-knew-about-init-self-super-and-abstract-classes-7103c8b91128
What I Wish I Knew About __init__, self, super(), and Abstract Classes | by maria siagian | Medium
December 17, 2024 - When I started learning Object-Oriented Programming (OOP) in Python, these concepts — __init__, self, super(), and Abstract Base Classes…
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to use abstract classes in python
How to Use Abstract Classes in Python | Towards Data Science
January 21, 2025 - The thing to note here is that it doesn’t even go to the __init__ method to check that. The fact that it has an abstract method that has not been implemented takes precedence and it fails because of that! This is how one would use the BasicPokemon class.
🌐
CodeSignal
codesignal.com › learn › courses › revisiting-oop-concepts-in-python › lessons › understanding-abstract-classes-and-abstract-methods-in-python
Understanding Abstract Classes and Abstract Methods in ...
Here, the Shape class is an abstract class that sets a blueprint with two abstract methods: area and perimeter. Subclasses must implement these methods. The Circle class inherits from Shape, using its constructor to initialize the radius and providing specific implementations for calculating ...
🌐
Earthly
earthly.dev › blog › abstract-base-classes-python
Abstract Base Classes in Python - Earthly Blog
July 19, 2023 - The Dog, Cat, and Bird classes indicated in the illustration are concrete subclasses that inherit from the Animal ABC and provide implementations for both abstract methods. To show how they will be implemented we will just implement just one subclass but the idea is the same for the other subclasses. Here is an example of the implementation of the Bird subclass. # animal.py class Bird(Animal): def __init__(self, name): self.name = name def get_name(self): return self.name def make_sound(self): return "Chirp chirp!"
🌐
Nscvce
nscvce.com.au › lesson › python-abstractclasses
Python Abstract Classes | NSCVCE
In Python, defining an abstract class entails using the abstractmethod decorator and subclassing from ABC. By doing this, programmers can lay the groundwork for other classes by defining the methods that need to be used ... from abc import ABC, abstractmethod class Vehicle(ABC): def __init__(self, ...
🌐
Scaler
scaler.com › home › topics › abstract class in python
Abstract Class in Python - Scaler Topics
April 9, 2024 - We can use the following syntax to create an abstract method in Python: We just need to put this decorator over any function we want to make abstract, and the abc module takes care of the rest. Now, let's take following example to demonstrate abstract classes:
Top answer
1 of 13
760

Use the abc module to create abstract classes. Use the abstractmethod decorator to declare a method abstract, and declare a class abstract using one of three ways, depending upon your Python version.

In Python 3.4 and above, you can inherit from ABC. In earlier versions of Python, you need to specify your class's metaclass as ABCMeta. Specifying the metaclass has different syntax in Python 3 and Python 2. The three possibilities are shown below:

Copy# Python 3.4+
from abc import ABC, abstractmethod
class Abstract(ABC):
    @abstractmethod
    def foo(self):
        pass
Copy# Python 3.0+
from abc import ABCMeta, abstractmethod
class Abstract(metaclass=ABCMeta):
    @abstractmethod
    def foo(self):
        pass
Copy# Python 2
from abc import ABCMeta, abstractmethod
class Abstract:
    __metaclass__ = ABCMeta

    @abstractmethod
    def foo(self):
        pass

Whichever way you use, you won't be able to instantiate an abstract class that has abstract methods, but will be able to instantiate a subclass that provides concrete definitions of those methods:

>>> Abstract()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Abstract with abstract methods foo
>>> class StillAbstract(Abstract):
...     pass
... 
>>> StillAbstract()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class StillAbstract with abstract methods foo
>>> class Concrete(Abstract):
...     def foo(self):
...         print('Hello, World')
... 
>>> Concrete()
<__main__.Concrete object at 0x7fc935d28898>
2 of 13
152

The old-school (pre-PEP 3119) way to do this is just to raise NotImplementedError in the abstract class when an abstract method is called.

Copyclass Abstract(object):
    def foo(self):
        raise NotImplementedError('subclasses must override foo()!')

class Derived(Abstract):
    def foo(self):
        print 'Hooray!'

>>> d = Derived()
>>> d.foo()
Hooray!
>>> a = Abstract()
>>> a.foo()
Traceback (most recent call last): [...]

This doesn't have the same nice properties as using the abc module does. You can still instantiate the abstract base class itself, and you won't find your mistake until you call the abstract method at runtime.

But if you're dealing with a small set of simple classes, maybe with just a few abstract methods, this approach is a little easier than trying to wade through the abc documentation.

🌐
Upgrad
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
September 12, 2024 - Think of it as a blueprint—you define what methods must exist, but not how they work. Any class that inherits from it must implement those methods. Python provides this through the abc module (abc stands for Abstract Base Class)