Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:48:30+00:00 2026-06-15T05:48:30+00:00

How can I make a class or method abstract in Python? I tried redefining

  • 0

How can I make a class or method abstract in Python?

I tried redefining __new__() like so:

class F:
    def __new__(cls):
        raise Exception("Unable to create an instance of abstract class %s" %cls)

But now, if I create a class G that inherits from F like so:

class G(F):
    pass

Then, I can’t instantiate G either, since it calls its super class’s __new__ method.

Is there a better way to define an abstract class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:48:32+00:00Added an answer on June 15, 2026 at 5:48 am

    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:

    # Python 3.4+
    from abc import ABC, abstractmethod
    class Abstract(ABC):
        @abstractmethod
        def foo(self):
            pass
    
    # Python 3.0+
    from abc import ABCMeta, abstractmethod
    class Abstract(metaclass=ABCMeta):
        @abstractmethod
        def foo(self):
            pass
    
    # 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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make spreadsheet with class/method modifiers. The spreadsheet itself can be located
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
what can I do to make the abstract function work in the emaillogger class?
Can we make a class copy constructor virtual in C++? How to use?
How can I make my class library STA for use with CreateObject in VBScript?
How can I specify the position of a JOptionPane. Can anyone make a class
To make a class immutable what I can do is: 1)Make class final 2)do
how can i make my custom class serializable and savable into IsolatedStorageSettings as value
I know from Algebra class that with ABC and 123 we can make 216
Let's see if I can make the step-by-step clear: namespace InventoryManager.Controllers { public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.