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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:14:43+00:00 2026-05-21T11:14:43+00:00

I have a base class and a few derived in Python: class Base: def

  • 0

I have a base class and a few derived in Python:

class Base:
   def Foo(self):
      pass

# First derived class
class Der1(Base):
   def OwnFoo(self):
      # Do something 1
   def OwnFoo2(self):
      # Do something 2

   def Foo(self):
      # Do something 3

# Second derived class
class Der2(Base):
   def OwnFoo(self):
      # Do something 1
   def OwnFoo2(self):
      # Do something 2

   def Foo(self):
      # Do something 3

The question is:

I have some predefined code in Der1. Almost all functions from Der2 do the same. How can I write this with less code?

I can’t add that code to the parent. Parent class shouldn’t be touched.

For example, Der2.OwnFoo does the same as Der1.OwnFoo, maybe there is some construction in python just to call OwnFoo from first class and not to write that code again?


I can’t change the parent of Der1 and Der2! It should be Base.

  • 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-21T11:14:44+00:00Added an answer on May 21, 2026 at 11:14 am

    Since you can’t change the inheritance structure, make a helper class that contains the common code and include it by composition rather than inheritance.

    # Common code goes in this new class
    class DerHelper:
        def __init__(self, parent):
            self._parent = parent
        def OwnFoo(self):
            print 'Do something 1', self._parent
        def OwnFoo2(self):
            print 'Do something 2', self._parent
        def Foo(self):
            print 'Do something 3', self._parent
    
    # First derived class
    class Der1(Base):
        def __init__(self):
            # include helper class by composition
            self._helper = DerHelper('Der1')
        def OwnFoo(self):
            self._helper.OwnFoo()
        def OwnFoo2(self):
            self._helper.OwnFoo2()
        def Foo(self):
            self._helper.Foo()
    
    # Second derived class
    class Der2(Base):
        def __init__(self):
            # include helper class by composition
            self._helper = DerHelper('Der2')
        def OwnFoo(self):
            self._helper.OwnFoo()
        def OwnFoo2(self):
            self._helper.OwnFoo2()
        def Foo(self):
            self._helper.Foo()
    

    Of course, you could pass a reference to the parent instead of a string. I just did it this way for demonstration purposes.

    Usage:

    d = Der1()
    d.OwnFoo()
    d.OwnFoo2()
    d.Foo()
    
    d = Der2()
    d.OwnFoo()
    d.OwnFoo2()
    d.Foo()
    

    Output:

    Do something 1 Der1
    Do something 2 Der1
    Do something 3 Der1
    Do something 1 Der2
    Do something 2 Der2
    Do something 3 Der2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class, FooBase. On it are a few standard read/write properties.
At work we have a base class, let's call it IntrusiveBase, that acts something
I have a base class and a derived class both in design phase. The
I have a base class object array into which I have typecasted many different
I have a base class with an optional virtual function class Base { virtual
I have a base class that represents a database test in TestNG, and I
I have a base class with a property which (the get method) I want
I have a base class that has a private static member: class Base {
I have a base class vehicle and some children classes like car, motorbike etc..
I have a base class in which I want to specify the methods a

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.