Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : How to "merge" two class

I want to add some attributes and methods into various class. The methods and attributes that I have to add are the same but not the class to assign them, so I want to construct a class who assign new methods and attributes for a class given in argument. I try this but it's not working: (I know that is a very wrong way to try to assign something to self, it's just to show what I want to do)

class A:
    def __init__(self):
        self.a = 'a'

    def getattA(self):
        return self.a

class B:
    def __init__(self, parent) :
        self = parent

        # This is working :
        print self.getattA()

    def getattB(self):
        return self.getattA()

insta = A()
instb = B(insta)

# This is not working :
print instb.getattB()

The result is :

a
Traceback (most recent call last):
  File "D:\Documents and settings\Bureau\merge.py", line 22, in <module>
    print instb.getattB()
  File "D:\Documents and settings\Bureau\merge.py", line 16, in getattB
    return self.getattA()
AttributeError: B instance has no attribute 'getattA'

And I expected to got 'a' for the call of instb.gettattB()

To resume I want to inherit class B from class A giving class A in argument of class B because my class B will be a subclass of various class, not always A.

like image 867
user1062526 Avatar asked Mar 12 '12 13:03

user1062526


People also ask

How to merge classes in Python?

With Python 3.9, a new operator is introduced called merge operator ( | ) in the dict class. To combine the dictionaries in a single line of code, use the merge operator. Python merge operator is also called a union operator.

Can you have two classes in Python?

A class can be derived from more than one base class in Python, similar to C++. This is called multiple inheritance. In multiple inheritance, the features of all the base classes are inherited into the derived class.

How do you concatenate objects in Python?

The most Pythonic way to concatenate a list of objects is the expression ''. join(str(x) for x in lst) that converts each object to a string using the built-in str(...) function in a generator expression. You can concatenate the resulting list of strings using the join() method on the empty string as a delimiter.


1 Answers

I was having trouble with calling different constructors, using super doesn't necessarily make sense in a case like this, I opted to inherit and call each constructor on the current object manually:

class Foo(object):
    def __init__(self, foonum):
        super(Foo, self).__init__()
        self.foonum = foonum


class Bar(object):
    def __init__(self, barnum):
        super(Bar, self).__init__()
        self.barnum = barnum

class DiamondProblem(Foo, Bar):
    # Arg order don't matter, since we call the `__init__`'s ourself.
    def __init__(self, barnum, mynum, foonum):
        Foo.__init__(self, foonum)
        Bar.__init__(self, barnum)

        self.mynum = mynum
like image 165
ThorSummoner Avatar answered Oct 02 '22 23:10

ThorSummoner