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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:58:12+00:00 2026-06-04T05:58:12+00:00

I am pondering if I should use inheritance or delegation to implement a kind

  • 0

I am pondering if I should use inheritance or delegation to implement a kind of wrapper class. My problem is like this: Say I have a class named Python.

class Python:

    def __init__(self):
        ...

    def snake(self):
        """ Make python snake through the forest"""
        ...

    def sleep(self):
        """ Let python sleep """
        ...

… and much more behavior. Now I have existing code which expects an Anaconda, which is almost like a Python, but slightly different: Some members have slightly different names and parameters, other members add new functionality. I really want to reuse the code in Python. Therefore I could do this with inheritance:

class Anaconda(Python):

    def __init__(self):
        Python.__init__(self)

    def wriggle(self):
        """Different name, same thing"""
        Python.snake(self)

    def devourCrocodile(self, croc):
        """ Python can't do this"""
        ...

Of course I can also call Anaconda().sleep(). But here is the problem: There is a PythonFactory which I need to use.

class PythonFactory:

    def makeSpecialPython(self):
        """ Do a lot of complicated work to produce a special python"""
        …
        return python

I want it to make a Python and then I should be able to convert it to an Anaconda:

myAnaconda = Anaconda(PythonFactory().makeSpecialPython())

In this case, delegation would be the way to go. (I don’t know whether this can be done using inheritance):

class Anaconda:

    def __init__(self, python):
        self.python = python

    def wriggle(self):
        self.python.wriggle()


    def devourCrocodile(self, croc):
        ...

But with delegation, I cannot call Anaconda().sleep().

So, if you’re still with me, my questions are:

A) In a case similar to this, where I need to

  • add some functionality
  • rename some functionality
  • use “base class” functionality otherwise
  • convert “base class” object to “subclass” object

should I use inheritance or delegation? (Or something else?)

B) An elegant solution would be to use delegation plus some special method that forwards all attribute and method accesses which Anaconda does not respond to to its instance of Python.

  • 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-04T05:58:14+00:00Added an answer on June 4, 2026 at 5:58 am

    B) An elegant solution would be to use delegation plus some special method that forwards all attribute and method accesses which Anaconda does not respond to to its instance of Python.

    This is simple in Python, just define __getattr__:

    class Anaconda:
    
        def __init__(self, python):
            self.python = python
    
        def wriggle(self):
            self.python.snake()
    
    
        def devourCrocodile(self, croc):
            ...
    
        def __getattr__(self, name):
            return getattr(self.python, name)
    

    See the Python docs on __getattr__

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

Sidebar

Related Questions

I have been pondering on whether or not I should use the typedefs inside
I have been pondering a lot lately over, why should one use tomcat manager
I'm wondering if I should use const string when using code like this: public
I have been using Jquery alot lately an was wondering if I should use
I'm wondering why should I use a class that inherits from the EventArgs class
I have a corruption memory heap with my application. I would like to use
I'm just wondering why should I use this method to return a function :
I came across the function InterlockedExchange and was wondering when I should use this
I'm wondering if I should use OpenId for my website. My first exposure to
I'm currently programming a client application and I'm wondering whether I should use the

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.