1

I'm learning object oriented programming in python and I'm not too sure how to write methods for classes

  1. My first question is, can you inherit two classes at the same time? Example:

    class A:
        def __init__ eg. storing list of strings 
    
    class B:
        def __ init__ eg. storing list of strings
    
    # I want to inherit both class A and B into class C
    class C (A,B): 
    

    Is this possible? many examples showed only inheritance from one class.

  2. Assuming that I can import the methods from both class A and class B, I need to combine the instances in class A and instances in class B. For example, class A stores list of names, class B stores a list of hair colours . In class C I want to add names and hair colours together so that I can tell who's having what hair colour. I'm not too sure how to add those two objects together but here's my attempt. Can you help me by giving suggestions of how to tackle this?

    class A: 
        def __init__(self,name):
            self.name= name
        def getName(self):
            return self.name # this is so that i whenever i call the object in my class C, it will return the name
    
    class B:
        def __init__(self,hair):
            self.hair = hair
        def getHair (self):
            return self.hair
    
    class C(A,B):
        def __init__(self):
            A.__init__(self,name)
            B.__init__(self,hair)
    
            self.add= [self,A,B]
            def add(self,name,hair): # my method to combine instances in class A and B
                self.add.append[name,hair] 
    

After solving this add issue, i.e. storing information of who's having what hair colour, my goal is to create another method in class C which will list me all the names that have the same hair colour when I provide the hair colour argument. I cannot do this without adding the two instances first.

I've tried to run this by giving each of the class several objects. It was ok for class A and class B, I can get the program to return me the name and the hair colour. But the problem came after I was trying to execute the .add part. It gives me a message error that says

TypeError: 'list' object is not callable

Please tell me whether the whole thing is wrong and I should start writing using different approach or my program can still be rescued.

3
  • Regarding your first question: yes, you can do that. It is known as multiple inheritance. Commented Dec 7, 2013 at 23:00
  • 2
    Note that with the way you have defined the function add, it is not visible outside the class. You need decrease the indent on the function in order for it to be visible Commented Dec 7, 2013 at 23:03
  • 1
    I would recommend that you favor composition over inheritance in cases like this. Commented Dec 7, 2013 at 23:05

1 Answer 1

1

First question. Answer is yes.

You need something after the class definition. Sometimes all the behaviour is defined by A and B, so you just need to put pass there (or a docstring)

class C(A, B):
    pass

Second question: append is a function, you need to call it with parentheses

self.add.append((name, hair))

You can't have an attribute add and a method add as they share the same namespace. Whichever one you define last will replace the other

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.