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
🌐
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.
🌐
Dive into Python
diveintopython.org › home › learn python programming › classes in python › class super() function
Using of super in Python Class Inheritance and init Method
May 3, 2024 - In this example, MyAbstractClass is an abstract class that defines an abstractmethod() called my_abstract_method(). The MyClass class inherits from MyAbstractClass and defines its own implementation of my_abstract_method(). When a new object of MyClass is created, its __init__() method calls super().__init__(), which initializes the abstract class MyAbstractClass. Overall, the super() function is a powerful tool in Python that can simplify inheritance hierarchies and streamline object initialization.
Discussions

[abc] Add abstract attributes via `abstract` type-hint - Ideas - Discussions on Python.org
Feature or enhancement Add a special generic type hint abstract, that allows specifying that subclasses must implement an attribute. from abc import ABC, abstract class Foo(ABC): myattr: abstract[int] # 100 upvotes) How to create abstract properties in python a... More on discuss.python.org
🌐 discuss.python.org
8
April 24, 2023
abstract class, how to create it properly?
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. More on reddit.com
🌐 r/learnpython
4
2
March 24, 2024
Enforcing __init__ signature when implementing it as an abstractmethod - Typing - Discussions on Python.org
I noticed that Pyright doesn’t ... 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...... More on discuss.python.org
🌐 discuss.python.org
1
December 29, 2024
Can I use __new__ for implementing an abstact base class?
I have a base class which aims at providing common implementation and this class should not be instantiated directly. For the time being this class does not have any abstract methods. Therefore even if I declare it as ab… More on discuss.python.org
🌐 discuss.python.org
0
0
November 14, 2023
🌐
Python Tutorial
pythontutorial.net › home › python oop › python abstract classes
Python Abstract Class
March 31, 2025 - Use abc module to define abstract classes. Was this tutorial helpful ? Yes No · Previously · Python __slots__ Up Next · Python Protocol · Python Object-oriented Programming · Class · Class Variables · Instance Methods · __init__: Initializing Instance Attributes ·
🌐
Python.org
discuss.python.org › ideas
[abc] Add abstract attributes via `abstract` type-hint - Ideas - Discussions on Python.org
April 24, 2023 - Feature or enhancement Add a special generic type hint abstract, that allows specifying that subclasses must implement an attribute. from abc import ABC, abstract class Foo(ABC): myattr: abstract[int] # 100 upvotes) How to create abstract properties in python a...
🌐
DEV Community
dev.to › sarahs › abstract-classes-in-python-55mj
Abstract Classes in Python - DEV Community
December 20, 2023 - The object indicates that Book is a new-style class inheriting from the base object class in Python. The line def __init__(self, title, author) is the constructor method for the class.
🌐
W3Schools
w3schools.com › python › python_classes.asp
Python Classes
Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects.
Find elsewhere
🌐
k0nze
k0nze.dev › posts › python-interfaces-abstract-classes
Python’s Abstract Base Classes (ABC) and Interfaces Explained (With Code Snippets) | k0nze
February 22, 2024 - From abc the class ABC is imported, which is the Abstract Base Class from which all abstract classes inherit. The methods and attributes of an abstract class in Python can be defined as in any other regular class.
🌐
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?

🌐
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 - What is an Abstract Class? An abstract class is a class, but not one you can create objects from directly. Its purpose is to define how other classes should look like, i.e. what methods and properties they are expected to have. The methods and properties defined (but not implemented) in an abstract class are called […]
🌐
Tutorialspoint
tutorialspoint.com › python › python_abstract_base_classes.htm
Python - Abstract Base Classes
An Abstract Base Class (ABC) in Python is a class that cannot be instantiated directly and is intended to be subclassed. ABCs serve as blueprints for other classes by providing a common interface that all subclasses must implement.
🌐
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
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-the-factory-pattern-in-python-a-practical-guide
How to Use the Factory Pattern in Python - A Practical Guide
February 9, 2026 - Here, the PaymentProcessor class defines an interface that all payment processors must implement. The @abstractmethod decorator marks methods that subclasses must override. You cannot instantiate PaymentProcessor directly. It only serves as a blueprint. All concrete processors (CreditCardProcessor, PayPalProcessor) must implement both process_payment and refund methods. If they don't, Python will raise an error.
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly created class instance.
🌐
Python.org
discuss.python.org › typing
Enforcing __init__ signature when implementing it as an abstractmethod - Typing - Discussions on Python.org
December 29, 2024 - I noticed that Pyright doesn’t ... 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......
🌐
Pydantic
docs.pydantic.dev › latest › concepts › models
Models - Pydantic Validation
Pydantic can validate data in three different modes: Python, JSON and strings. ... The __init__() model constructor.
🌐
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 ...
🌐
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 - Let’s break down these key elements in simple terms with examples: The __init__ method is a special method in Python that is automatically called when you create a new object from a class.
🌐
Python.org
discuss.python.org › python help
Can I use __new__ for implementing an abstact base class? - Python Help - Discussions on Python.org
November 14, 2023 - I have a base class which aims at providing common implementation and this class should not be instantiated directly. For the time being this class does not have any abstract methods. Therefore even if I declare it as abstract, someone can instantiate it (see the example below): import abc class BaseURL(metaclass=abc.ABCMeta): def __init__(self, path: str) -> None: self.path = path def __init_subclass__(cls, schema: str) -> None: cls.schema = schema def __str__(s...
🌐
Python
docs.python.org › 3 › library › abc.html
abc — Abstract Base Classes
Source code: Lib/abc.py 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. (See also ...