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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:46:53+00:00 2026-06-08T12:46:53+00:00

I am trying to avoid repeating the same code in the clone methods of

  • 0

I am trying to avoid repeating the same code in the clone methods of two classes, one inheriting from the other. My current approach is to put the code that is common to both classes in the (non-static) clone method of the base class and call it from the extending class. To do this I am trying to pass the constructor of the extending class to the base class so it can be invoked there. Here is a simplified example of what I am currently doing:

class S(object):
    """Base class"""
    def __init__(self):
        self.a = 0

    def clone(self, constructor=None):
        if constructor is None:
            constructor = self.__init__
        cloned = constructor()
        # Expecting: cloned.a = 1, cloned.b = 7

        assert cloned is not None  # raises AssertionError
        cloned.a = self.a  # Set a to 2
        return cloned

class C(S):
    """Child class, extending Base"""
    def __init__(self):
        self.a = 1
        self.b = 7

    def clone(self):
        cloned = super(C, self).clone(self.__init__)
        # Expecting: cloned.a = 2, cloned.b = 7

        cloned.b = self.b  # Set b to 8
        return cloned

if __name__ == '__main__':
    c1 = C()
    c1.a = 2
    c1.a = 8
    c2 = c1.clone()

My questions at this point are:

  • Why is None returned when calling the clone method of the base class?

  • Do I have to bind the method to a class in order to use it, using
    types.MethodType or __get__?

  • What would you suggest as a better way to go?

  • 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-08T12:46:55+00:00Added an answer on June 8, 2026 at 12:46 pm

    While __init__() is often called the constructor in Python, it actually isn’t. It gets the instance passed in as the self argument, and it returns None. The instance is constructed earlier by the __new__() method, which is the actual constructor. You shouldn’t pass in a reference to __new__, though. It’s much easier and more straightforward to pass in the type itself.

    Edit: The right approach to implement a clone method is yet another one: Stick to the usual Python protocols, and implement __getstate__() and __setstate__() on your instances. Then you can use the functions from the copy module to clone your instances correctly.

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

Sidebar

Related Questions

I am trying to refactor some methods to avoid repeating myself in the object
Also trying to avoid any use of plugins, (apart from jQuery of course, whether
I'm probably over-thinking this/wasting time trying to avoid a bit of conditional code -
I'm trying to select 2 of 3 existing a elements from a repeating HTML
I need a way to dynamically populate this query... to avoid repeating the same
Trying to avoid the SomethingManager trap here... Let's say I'm going to write a
I am trying to avoid C6011 warning because an abort function is calling exit().
I'm trying to avoid the use of magic strings as much as I can,
I'm trying to avoid the memory leak in PHP. When I create a object
I'm trying to avoid using Interface Builder as much as possible. At the moment

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.