creating a method in object oriented programming with python

本秂侑毒 提交于 2019-12-25 02:44:39

问题


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.


回答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



来源:https://stackoverflow.com/questions/20447745/creating-a-method-in-object-oriented-programming-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!