I'm learning object oriented programming in python and I'm not too sure how to write methods for classes
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.
Assuming that I can import the methods from both
class Aandclass B, I need to combine the instances inclass Aand instances inclass B. For example,class Astores list of names,class Bstores a list of hair colours . Inclass CI 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.
add, it is not visible outside the class. You need decrease the indent on the function in order for it to be visible