How to Fix Python Error ‘Can’t Instantiate Abstract Class’

Here’s the fix for can’t instantiate abstract class error. Also explained the reason for it.

A class that is derived from the ABC class belonging to the ABC module is called an abstract class. ABC class a.k.a metaclass. Precisely, it is a class that defines the behavior of other classes. So it can be said that the class which is defined from the ABC class becomes a metaclass.


Abstract Class

When you supply ‘ABC’ to any class then, that class becomes an abstract class. The other condition is it should have an ‘abstract method’. This is a mandatory requirement. It can have both abstract methods and concrete methods. The abstract method means it has just a template. But the concrete method has a definition.

Rules for Abstract class

  • The objects of an abstract class cannot be created by PVM (python virtual machine).
  • It is not mandatory to declare all the methods as abstract in an abstract class.
  • The class must be abstract if there is any abstract method in a class.
  • Defining the abstract methods of an abstract class must be in its child class/subclass.
  • If any abstract class is inherited which have an abstract method, then either implementation of the method or making this class abstract must be provided.
  • An abstract class can have an abstract method and a concrete method.

Also read: CFI’s all-access subscription (self-study and full-immersion)

Abstract class example threw an error

from abc import ABC,abstractmethod

class one(ABC):
    def move(self):

When you run this code, it throws an error. The error is “TypeError: Can’t instantiate abstract class one with abstract methods move

TypeError: Can't instantiate abstract class one with abstract methods move
Python code

Reasons for error

The abstract method is not implemented (that should be implemented in the child class). That’s why we got this error. Here’s the code after fixing the error.

Implementing the abstract method

Code is fixed and the error ‘Can’t Instantiate Abstract Class’ is not displayed now.

References: Object Oriented Programming with Python-3

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.