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 6110777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:33:17+00:00 2026-05-23T14:33:17+00:00

When designing classes abstract methods can be very helpful. From what I know, Python

  • 0

When designing classes abstract methods can be very helpful. From what I know, Python does not have a mechanism for enforcing an inherited class to implement the abstract method. In my code (see example below) I enter a failed assertion in the base class to cause a runtime error if not implemented. Is this unpythonic?

class Dog(Animal):
  def speak(self):
   return "bark"

class Animal():
  def speak(self):
   assert(False) #abstract
  • 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-23T14:33:17+00:00Added an answer on May 23, 2026 at 2:33 pm

    Python actually does have abstract classes with abstact methods:

    >>> import abc
    >>> 
    >>> class IFoo(object):
    ...     __metaclass__ = abc.ABCMeta
    ...     
    ...     @abc.abstractmethod
    ...     def foo(self):
    ...         pass
    ... 
    >>> foo = IFoo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Cant instantiate abstract class IFoo with abstract methods foo
    >>> class FooDerived(IFoo):
    ...     pass
    ... 
    >>> foo = FooDerived()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Cant instantiate abstract class FooDerived with abstract methods foo
    >>> class FooImplements(FooDerived):
    ...     def foo(self):
    ...         print "foo'ed"
    ... 
    >>> foo = FooImplements()
    >>> foo.foo()
    foo'ed
    >>> 
    

    On the other hand, the fundamental question of “is this pythonic” is a bit harder to say. If your intent is to provide the abstract base class so that you can later check to make sure that values inherit from it, then no, that’s not particularly pythonic, even though it’s possible to make arbitrary types abstract subclasses of your baseclass. On the other hand, it’s perfectly fine to provide an abstract baseclass that implements some functionality based upon the implementation provided in concrete subclasses. For example, collections.Sequence and collections.Mapping do just this for list like and dict like classes; subclasses can provide __getitem__ and can get __contains__ and others for free.

    For certain, you should never use assert() except to document the expectations of code; If it’s actually possible for the assert to fail, you shouldn’t be using an assert. Optimized python (python -O script.py) does not check assertions.

    Edit: more exposition:

    If you are checking the type of a value:

    def foo(bar):
        if not isinstance(bar, AbstractBaz):
            raise ValueError, ("bar must be an instance of AbstractBaz, "
                               "got %s" % type(bar))
    

    If for some reason you can’t use @abstractmethod, but still want that effect, you should raise NotImplementedError. You might want to do this because you actually do want instances of that class, some of which might not need to implement optional functionality. You still should account for the possibility that the function was called through super(). To a first approximation, that might look like this.

    class Foo(object):
        def bar(self, baz):
            if self.bar.im_func == Foo.bar.im_func:
                raise NotImplementedError, "Subclasses must implement bar"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm designing this collection of classes and abstract (MustInherit) classes… This is the database
I have trouble when designing classes like this class C1 { public: void foo();
How can you avoid circular dependencies when you're designing two classes with a producer/consumer
I am designing some immutable classes but I have to have some variables like
I have an abstract immutable base class that defines enforces child classes to be
I have an abstract class CommandPath, and a number of derived classes as below:
Following up from this question: designing application classes What is wrong (from a design
I am designing a loosely-coupled structure. I want to call classes from different assemblies/namespaces
When designing classes you usually have to decide between: providing a full constructor that
I got some difficulties designing my case classes. A simplified version looks like: abstract

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.