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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:20:28+00:00 2026-05-16T21:20:28+00:00

I’ve got a base class where I want to handle __add__() and want to

  • 0

I’ve got a base class where I want to handle __add__() and want to support when __add__ing two subclass instances – that is have the methods of both subclasses in the resulting instance.

import copy

class Base(dict):
    def __init__(self, **data):
        self.update(data)

    def __add__(self, other):
        result = copy.deepcopy(self)
        result.update(other)
        # how do I now join the methods?
        return result

class A(Base):
    def a(self):
        print "test a"

class B(Base):
    def b(self):
        print "test b"


if __name__ == '__main__':
    a = A(a=1, b=2)
    b = B(c=1)
    c = a + b
    c.b() # should work
    c.a() # should work

Edit: To be more specific: I’ve got a class Hosts that holds a dict(host01=.., host02=..) (hence the subclassing of dict) – this offers some base methods such as run_ssh_commmand_on_all_hosts()

Now I’ve got a subclass HostsLoadbalancer that holds some special methods such as drain(), and I’ve got a class HostsNagios that holds some nagios-specific methods.

What I’m doing then, is something like:

nagios_hosts = nagios.gethosts()
lb_hosts = loadbalancer.gethosts()
hosts = nagios_hosts + lb_hosts
hosts.run_ssh_command_on_all_hosts('uname')
hosts.drain() # method of HostsLoadbalancer - drains just the loadbalancer-hosts
hosts.acknoledge_downtime() # method of NagiosHosts - does this just for the nagios hosts, is overlapping

What is the best solution for this problem?

I think I can somehow “copy all methods” – like this:
for x in dir(other):
setattr(self, x, getattr(other, x))

Am I on the right track? Or should I use Abstract Base Classes?

  • 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-16T21:20:28+00:00Added an answer on May 16, 2026 at 9:20 pm

    In general this is a bad idea. You’re trying to inject methods into a type. That being said, you can certainly do this in python, but you’ll have to realize that you want to create a new type each time you do this. Here’s an example:

    import copy
    
    class Base(dict):
        global_class_cache = {}
    
        def __init__(self, **data):
            self.local_data = data
    
        def __add__(self, other):
            new_instance = self._new_type((type(self), type(other)))()
            new_instance.update(copy.deepcopy(self).__dict__)
            new_instance.update(copy.deepcopy(other).__dict__)
            return new_instance
    
        def _new_type(self, parents):
            parents = tuple(parents)
            if parents not in Base.global_class_cache:
                name = '_'.join(cls.__name__ for cls in parents)
                Base.global_class_cache[parents] = type(name, parents, {})
            return Base.global_class_cache[parents]
    
    class A(Base):
        def a(self):
            print "test a"
    
    class B(Base):
        def b(self):
            print "test b"
    
    
    if __name__ == '__main__':
        a = A(a=1, b=2)
        b = B(c=1)
        c = a + b
        c.b() # should work
        c.a() # should work
        print c.__class__.__name__
    

    UPDATE
    I’ve updated the example to remove manually moving the methods — we’re using mixins here.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have two tables with like below codes: Table: Accounts id | username |
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.