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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:11:54+00:00 2026-05-27T22:11:54+00:00

I have a class in the main project I don’t want to change. class

  • 0

I have a class in the main project I don’t want to change.

class A():
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def name(self):
        # this method could be much more complex
        return self.lastname.upper()

I’m trying to build a plugin mechansim. So far so good, I have an extension point like this:

if __name__ == '__main__':
    ''' The main project has an extension point that allows me to do'''
    # for each class extension such as AExtended:
    A.name = AExtended.name
    ''' After the extensions are loaded, some behaviours may be changed'''
    a = A("John", "Doe")
    print(a.name())

A plugin can be written like this:

class AExtended(A):
    ''' This is an extension I provide through a plugin mechanism
    '''
    def name(self):
        return self.firstname + ' ' + self.lastname.upper()

This all works very well. I now get “John DOE”.

My problem is that the original name() method can be quite complex. In other words, I can’t afford to call self.lastname.upper() in the AExtended. I’d like to call the “super” method, which does not exist any more, because it has been overwritten.

How can I change my code, in order to achieve something like this:

class AExtended(A):
    def name(self):
        # I'm looking for a way to call the base implementation that was in A.name()
        return self.firstname + ' ' + parent.name()

Thanks for your help!

Edit: Some explanations of what I try to do.

  • I want the plugin to patch the behaviour of A. I can’t afford to change existing consumers of A
  • There are many classes like A that could be changed, I’d like plugins to have full control and responsibility
  • It’s true AExtended does not have to inherit from A, but it was an easy way to access self.firstname. I have no problem following a different design pattern if it can help.

I have a workaround, but it’s not very elegant and hard to generalize

class AExtended(A):
    def name(self):
        # I'm looking for a way to call the base implementation that was in A.name()
        return self.firstname + ' ' + self.parentname()
#in main
A.parentname = A.name
A.name = AExtended.name
  • 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-27T22:11:54+00:00Added an answer on May 27, 2026 at 10:11 pm

    Here’s a full example of what I was hinting at. Feel free to yell at me and have me merge my answers, or downvote one or whatever, just easier to provide an alternative as a new answer. I’ll let the code do the talking instead of poorly explaining it. 🙂

    ## Some shared class that is used all over the place and needs to be patched.
    
    class A(object):
        def __init__(self):
            self.firstname = 'Bob'
    
        # Print my first name.
        def name(self):
            return self.firstname
    
        # Use this to allow patching arbitrary methods...
        @classmethod
        def patch(cls, func_name):
            def patch_by_name(new_func):
                old_func = getattr(cls, func_name)
                def patched_func(self):
                    return new_func(self, old_func)
                setattr(cls, func_name, patched_func)
            return patch_by_name
    
    ## Some other area of the code where you want to throw in a patch
    
    class PatchedA(A):  # doesn't need to subclass, but comes in handy sometimes
        @A.patch('name')
        def name(self, orig_func):
            return 'I am ' + orig_func(self) + 'McWizwaz'
    
    print 'Who are you, A class?'
    print A().name()  # prints 'I am Bob McWizwaz'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have any div for show details of project ( class main ). now
Let's say that my project has a main class. I'd love to have public
The situation is like this : Main project A. and a class library B.
I have a main class in Android java project where all functions are defined.
I have a main class and two extended classes: class Main { public $foo;
I have a class class with main thread public class MainThread extends Thread {
I have a Class(call it main class) and the method in main class calls
I have my main class PayUnit where I create my main object references, such
I have one main class with several inherited members who all overload the same
I have a main class in a program that launches another class that handles

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.