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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:32:10+00:00 2026-05-28T13:32:10+00:00

I need to refactor existing code by collapsing a method that’s copy-and-pasted between various

  • 0

I need to refactor existing code by collapsing a method that’s copy-and-pasted between various classed that inherit from one another into a single method.
So I produced the following code:

class A(object):
    def rec(self):
        return 1

class B(A):
    def rec(self):
        return self.rec_gen(B)

    def rec_gen(self, rec_class):
        return super(rec_class, self).rec() + 1

class C(B):
    def rec(self):
        return self.rec_gen(C)

if __name__=='__main__':
    b = B(); c = C()
    print c.rec()
    print b.rec()

And the output:

3
2

What still bothers me is that in the ‘rec’ method I need to tell ‘rec_gen’ the context of the class in which it’s running. Is there a way for ‘rec_gen’ to figure it out by itself in runtime?

  • 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-28T13:32:11+00:00Added an answer on May 28, 2026 at 1:32 pm

    This capability has been added to Python 3 – see PEP 3135. In a nutshell:

    class B(A):
        def rec(self):
            return super().rec() + 1
    

    I think you’ve created the convoluted rec()/rec_gen() setup because you couldn’t automatically find the class, but in case you want that anyway the following should work:

    class A(object):
        def rec(self):
            return 1
    
    class B(A):
        def rec(self):
            # __class__ is a cell that is only created if super() is in the method
            super()
            return self.rec_gen(__class__)
    
        def rec_gen(self, rec_class):
            return super(rec_class, self).rec() + 1
    
    class C(B):
        def rec(self):
            # __class__ is a cell that is only created if super() is in the method
            super()
            return self.rec_gen(__class__)
    

    The simplest solution in Python 2 is to use a private member to hold the super object:

    class B(A):
        def __init__(self):
            self.__super = super(B)
    
        def rec(self):
            return self.__super.rec() + 1
    

    But that still suffers from the need to specify the actual class in one place, and if you happen to have two identically-named classes in the class hierarchy (e.g. from different modules) this method will break.

    There were a couple of us who made recipes for automatic resolution for Python 2 prior to the existence of PEP 3135 – my method is at self.super on ActiveState. Basically, it allows the following:

    class B(A, autosuper):
        def rec(self):
            return self.super().rec() + 1
    

    or in the case that you’re calling a parent method with the same name (the most common case):

    class B(A, autosuper):
        def rec(self):
            return self.super() + 1
    

    Caveats to this method:

    1. It’s quite slow. I have a version sitting around somewhere that does bytecode manipulation to improve the speed a lot.

    2. It’s not consistent with PEP 3135 (although it was a proposal for the Python 3 super at one stage).

    3. It’s quite complex.

    4. It’s a mix-in base class.

    I don’t know if the above would enable you to meet your requirements. With a small change to the recipe though you could find out what class you’re in and pass that to rec_gen() – basically extract the class-finding code out of _getSuper() into its own method.

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

Sidebar

Related Questions

I've got some sample code that I'd like to refactor as I need it
I'm looking at existing python code that heavily uses Paramiko to do SSH and
I need to refactor this code to not use a parameter when using the
I feel the need to refactor my old CF5 based code into CFC's. We
I have a solution that is missing a lot of code coverage. I need
I have the following script that I need to adapt to read from a
I need to refactor a VBS dealing with LDAP, ADODB and ActiveDirectory from VBS
I have a problem with some code I need to refactor. Right now it
I have a set of $.get() requests that I need to refactor to include
I am just embarking on my first large-scale refactor, and need to split an

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.