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
If it returns False, the subclass is not considered a subclass of this ABC, even if it would normally be one. If it returns NotImplemented, the subclass check is continued with the usual mechanism. For a demonstration of these concepts, look at this example ABC definition:
🌐
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
Discussions

How define constructor implementation for an Abstract Class in Python? - Stack Overflow
The arguments passed to __new__ ... to __init__ after __new__ returns an instance. You don't want to forward them to object.__new__, because it doesn't accept them. 2017-06-28T11:30:18.62Z+00:00 ... Oh, nevermind, this works in python 2. Weird. Still, there's no reason to forward the parameters, is there? 2017-06-28T11:34:20.177Z+00:00 ... You should define the methods as abstract as well with the @abc.abstractmethod ... More on stackoverflow.com
🌐 stackoverflow.com
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
Python Abstract Method With It's own __init__ function - Stack Overflow
How can I define a __init__ function in both the base and derived abstract classes and have all self.* be available in the abstract method? For example: What is the proper way of utilizing functions that are imported in the base class of an abstract class? For example: in base.py I have the following: import abc ... More on stackoverflow.com
🌐 stackoverflow.com
__init__ in ABC abstract class in Python - Stack Overflow
In the below example, do I need to call the super().__init__() from the MySQLdatabase class or it is not needed? If I override the __init__ in the MySQLdatabase then it is my understanding that the __init__ of the super class is not called anymore. Does this mean that the __init__ of the ABC class ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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...
🌐
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 »
🌐
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 - It acts like a constructor in other programming languages and is used to initialise the object’s attributes. Think of __init__ as the blueprint setup for building a house. When you construct a house (create an object), you decide how many ...
🌐
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
🌐
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.
🌐
DEV Community
dev.to › sarahs › abstract-classes-in-python-55mj
Abstract Classes in Python - DEV Community
December 20, 2023 - ... 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, ...
🌐
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!"
🌐
MakeUseOf
makeuseof.com › home › programming › abstract classes in python: a beginner's guide
Abstract Classes in Python: A Beginner's Guide
September 11, 2021 - 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 ...
🌐
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")
🌐
Justin A. Ellis
jellis18.github.io › post › 2022-01-11-abc-vs-protocol
Abstract Base Classes and Protocols: What Are They? When To Use Them?? Lets Find Out! - Justin A. Ellis
January 11, 2022 - Below is a slightly more realistic example of a base class for a statistical or Machine Learning regression model · from abc import ABC, abstractmethod from typing import List, TypeVar import numpy as np T = TypeVar("T", bound="Model") class Model(ABC): def __init__(self): self._is_fitted = False def fit(self: T, data: np.ndarray, target: np.ndarray) -> T: fitted_model = self._fit(data, target) self._is_fitted = True return fitted_model def predict(self, data: np.ndarray) -> List[float]: if not self._is_fitted: raise ValueError(f"{self.__class__.__name__} must be fit before calling predict") return self._predict(data) @property def is_fitted(self) -> bool: return self._is_fitted @abstractmethod def _fit(self: T, data: np.ndarray, target: np.ndarray) -> T: pass @abstractmethod def _predict(self, data: np.ndarray) -> List[float]: pass
🌐
Geek Python
geekpython.in › abc-in-python
Python's ABC: Understanding the Basics of Abstract Base Classes
October 29, 2023 - Python will raise an error upon executing the above code because the class Sachin doesn’t follow the class Details blueprint. As we saw in the above example that if a derived class doesn’t follow the blueprint of the abstract class, then the error will be raised. That’s where ABC(Abstract Base Class) plays an important role in making sure that the subclasses must follow that blueprint.
🌐
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 - Let’s start with a simple example to see why ABCs are useful. Let’s create an abstract base class called Vehicle with two abstract methods start_engine() and get_fuel_type() that all vehicle types must implement, plus a concrete method get_info() that provides common vehicle information. ... from abc import ABC, abstractmethod # Abstract base class for vehicles class Vehicle(ABC): def __init__(self, brand, model): self.brand = brand self.model = model @abstractmethod def start_engine(self): pass @abstractmethod def get_fuel_type(self): pass # Concrete method - common functionality def get_info(self): return f"{self.brand} {self.model}"
🌐
GeeksforGeeks
geeksforgeeks.org › python › abstract-classes-in-python
Abstract Classes in Python - GeeksforGeeks
... from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def make_sound(self): pass # animal = Animal() # Raises: TypeError · Explanation: make_sound() is an abstract method in the Animal class, so it doesn't have any code ...
Published   September 3, 2025
🌐
Python
docs.python.org › 3 › library › collections.abc.html
collections.abc — Abstract Base Classes for Containers
For example, to write a class supporting the full Set API, it is only necessary to supply the three underlying abstract methods: __contains__(), __iter__(), and __len__(). The ABC supplies the remaining methods such as __and__() and isdisjoint(): ...
🌐
Plain English
python.plainenglish.io › understand-the-abc-class-and-the-magical-init-subclass-method-in-python-7d42ef99d993
Understand the ABC class and the magical __init_subclass__ method in Python | by Lynn G. Kwong | Python in Plain English
October 31, 2024 - The ABC class in Python stands for “Abstract Base Class” which is used to define abstract classes containing some methods that must be implemented by any subclass, ensuring that subclasses meet certain interface requirements. However, the base class itself cannot be instantiated. The __init_subclass__ method in Python is a special class method that is automatically called when a class is subclassed.