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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:53:00+00:00 2026-06-06T20:53:00+00:00

How do I create a decorator for an abstract class method in Python 2.7?

  • 0

How do I create a decorator for an abstract class method in Python 2.7?

Yes, this is similar to this question, except I would like to combine abc.abstractmethod and classmethod, instead of staticmethod. Also, it looks like abc.abstractclassmethod was added in Python 3 (I think?), but I’m using Google App Engine, so I’m currently limited to Python 2.7

Thanks in advance.

  • 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-06T20:53:02+00:00Added an answer on June 6, 2026 at 8:53 pm

    Here’s a working example derived from the source code in Python 3.3’s abc module:

    from abc import ABCMeta
    
    class abstractclassmethod(classmethod):
    
        __isabstractmethod__ = True
    
        def __init__(self, callable):
            callable.__isabstractmethod__ = True
            super(abstractclassmethod, self).__init__(callable)
    
    class DemoABC:
    
        __metaclass__ = ABCMeta
    
        @abstractclassmethod
        def from_int(cls, n):
            return cls()
    
    class DemoConcrete(DemoABC):
    
        @classmethod
        def from_int(cls, n):
            return cls(2*n)
    
        def __init__(self, n):
            print 'Initializing with', n
    

    Here’s what it looks like when running:

    >>> d = DemoConcrete(5)             # Succeeds by calling a concrete __init__()
    Initializing with 5
    
    >>> d = DemoConcrete.from_int(5)    # Succeeds by calling a concrete from_int()
    Initializing with 10
    
    >>> DemoABC()                       # Fails because from_int() is abstract    
    Traceback (most recent call last):
      ...
    TypeError: Can't instantiate abstract class DemoABC with abstract methods from_int
    
    >>> DemoABC.from_int(5)             # Fails because from_int() is not implemented
    Traceback (most recent call last):
      ...
    TypeError: Can't instantiate abstract class DemoABC with abstract methods from_int
    

    Note that the final example fails because cls() won’t instantiate. ABCMeta prevents premature instantiation of classes that haven’t defined all of the required abstract methods.

    Another way to trigger a failure when the from_int() abstract class method is called is to have it raise an exception:

    class DemoABC:
    
        __metaclass__ = ABCMeta
    
        @abstractclassmethod
        def from_int(cls, n):
            raise NotImplementedError
    

    The design ABCMeta makes no effort to prevent any abstract method from being called on an uninstantiated class, so it is up to you to trigger a failure by invoking cls() as classmethods usually do or by raising a NotImplementedError. Either way, you get a nice, clean failure.

    It is probably tempting to write a descriptor to intercept a direct call to an abstract class method, but that would be at odds with the overall design of ABCMeta (which is all about checking for required methods prior to instantiation rather than when methods are called).

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

Sidebar

Related Questions

I'd like to create a Python class decorator (*) that would be able to
I'd like to create a new decorator to use in place of @wraps(f) that
I have created this file at My/View/Helper/FormElement.php <?php abstract class My_View_Helper_FormElement extends Zend_View_Helper_FormElement {
I want to create a decorator that works like a property, only it calls
I have a decorator chain that looks like this when initially created: IType calculator
I'd like to create a decorator like below, but I can't seem to think
I want to create a decorator that profiles a method and logs the result.
I can't figure out why the permission required decorator isn't working. I would like
I'd like to create a decorator for Flask routes to flag certain routes as
Currently my controller looks like this: public class ProductBrandsController : Controller I've read online

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.