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. Answer from Rawing7 on reddit.com
GeeksforGeeks
geeksforgeeks.org › python › abstract-classes-in-python
Abstract Classes in Python - GeeksforGeeks
Python provides the abc module to define ABCs and enforce the implementation of abstract methods in subclasses. Example: This example shows an abstract class Animal with an abstract method sound() and a concrete subclass Dog that implements it.
Published September 3, 2025
Videos
04:57
Python Abstract Class and Abstract Method - YouTube
Python Tutorial for Beginners 35 - Python Abstract Classes
07:02
Learn Python ABSTRACT CLASSES in 7 minutes! 👻 - YouTube
10:05
Python Interfaces and Abstract Base Class (ABC): A Must-Know for ...
07:12
Python Inheritance and Abstract Classes | OOP - YouTube
21:27
Understanding Python: Abstract Base Classes - YouTube
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
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
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.
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")
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 Course
python-course.eu › oop › the-abc-of-abstract-base-classes.php
20. The 'ABC' of Abstract Base Classes | OOP | python-course.eu
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-2bcc42ab0b46> in <module> 2 pass 3 ----> 4 x = DoAdd42(4) TypeError: Can't instantiate abstract class DoAdd42 with abstract methods do_something · We will do it the correct way in the following example, in which we define two classes inheriting from our abstract 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 ...
For example, consider the following ... abstract methods: area and perimeter. Subclasses must implement these methods. The Circle class inherits from Shape, using its constructor to initialize ......
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...
Upgrad
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
September 12, 2024 - No, you cannot instantiate an abstract class in Python example directly.
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 - Some of you might be wondering why we couldn’t just use a normal class (i.e. not inherit from ABC) and raise a NotImplementerError for methods that have not been implemented, like in the example below. class NotAbstractBasicPokemon: def __init__(self, name): self.name = name self._level = 1
Python
pythonprogramminglanguage.com › abstract-base-classes
Abstract Base Classes - Python
If you are a Python beginner, then I highly recommend this book. Create an abstract class: AbstractAnimal.
Python.org
discuss.python.org › ideas
Provide a canonical way to declare an abstract class variable - Ideas - Discussions on Python.org
October 28, 2024 - There’s a recent help post of Abstract variables in abc that asks about how an “abstract variable” can be declared such that it is required for a subclass to override the variable, to which @drmason13 replied: Although…
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 class in Python: Here we just need to inherit the ABC class from the abc module in Python. Now, let's take the following example to demonstrate abstract classes: