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

  • SEARCH
  • Home
  • 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 996033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:48:56+00:00 2026-05-16T06:48:56+00:00

Suppose I want to create an abstract class in Python with some methods to

  • 0

Suppose I want to create an abstract class in Python with some methods to be implemented by subclasses, for example:

class Base():
    def f(self):
        print "Hello."
        self.g()
        print "Bye!" 

class A(Base):
    def g(self):
        print "I am A"

class B(Base):
    def g(self):
        print "I am B"

I’d like that if the base class is instantiated and its f() method called, when self.g() is called, that throws an exception telling you that a subclass should have implemented method g().

What’s the usual thing to do here? Should I raise a NotImplementedError? or is there a more specific way of doing it?

  • 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-05-16T06:48:57+00:00Added an answer on May 16, 2026 at 6:48 am

    In Python 2.6 and better, you can use the abc module to make Base an “actually” abstract base class:

    import abc
    
    class Base:
        __metaclass__ = abc.ABCMeta
        @abc.abstractmethod
        def g(self):
            pass
        def f(self): # &c
    

    this guarantees that Base cannot be instantiated — and neither can any subclass which fails to override g — while meeting @Aaron’s target of allowing subclasses to use super in their g implementations. Overall, a much better solution than what we used to have in Python 2.5 and earlier!

    Side note: having Base inherit from object would be redundant, because the metaclass needs to be set explicitly anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function def oldfunction(arg1,arg2,arg3): print(arg1,arg2,arg3) I want to create a new
Suppose I want to create a (stateless) WCF service with three methods exposed on
Suppose I want to create a functor that acts upon some general type. For
Let's suppose I want to create a javascript class/object/function which have a method that
I want to create sub-domains using PHP on the fly. Suppose a user registers
Suppose I want to execute code, for example value += 5 inside a namespace
Suppose I want to create my own desktop envoriment for Linux, without X. Like
Suppose I want to create a select box in PHP. $months = array ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
I want to create an object which represents an html document which is suppose
Suppose I have an immutable NSArray and want to create several sub-arrays. I could

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.