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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:03:48+00:00 2026-05-26T13:03:48+00:00

For example, I have a class BaseHandler(object): def prepare(self): self.prepped = 1 I do

  • 0

For example, I have a

class BaseHandler(object):
    def prepare(self):
        self.prepped = 1

I do not want everyone that subclasses BaseHandler and also wants to implement prepare to have to remember to call

super(SubBaseHandler, self).prepare()

Is there a way to ensure the superclass method is run even if the subclass also implements prepare?

  • 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-26T13:03:49+00:00Added an answer on May 26, 2026 at 1:03 pm

    I have solved this problem using a metaclass.

    Using a metaclass allows the implementer of the BaseHandler to be sure that all subclasses will call the superclasses prepare() with no adjustment to any existing code.

    The metaclass looks for an implementation of prepare on both classes and then overwrites the subclass prepare with one that calls superclass.prepare followed by subclass.prepare.

    class MetaHandler(type):
        def __new__(cls, name, bases, attrs):
            instance = type.__new__(cls, name, bases, attrs)
            super_instance = super(instance, instance)
            if hasattr(super_instance, 'prepare') and hasattr(instance, 'prepare'):
                super_prepare = getattr(super_instance, 'prepare')
                sub_prepare = getattr(instance, 'prepare')
                def new_prepare(self):
                    super_prepare(self)
                    sub_prepare(self)
                setattr(instance, 'prepare', new_prepare)
            return instance
    
    
    class BaseHandler(object):
        __metaclass__ = MetaHandler
        def prepare(self):
            print 'BaseHandler.prepare'
    
    
    class SubHandler(BaseHandler):
        def prepare(self):
            print 'SubHandler.prepare'
    

    Using it looks like this:

    >>> sh = SubHandler()
    >>> sh.prepare()
    BaseHandler.prepare
    SubHandler.prepare
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: I have an class that inherits from UIImageView. An object creates an instance
For example I have class Class Cls: var 1 var 2 def func(self): ---
For example I have a class that builds the GUI, a class that handles
I have a class Projectiles, and I want to create an object with it.
I want to get class methods in an object. Please see the following example
Let's say for example I have a class A that creates an instance of
I have a architecture question about legacy. For example I have a class that
I have a class, which has a bunch of subclasses that inherit from it.
Let's say I for example have this class that generates Fibonacci numbers: public class
For example suppose I have class Parent { def method() { var myvar =

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.