Home > Blockchain >  python polymorphism class and function
python polymorphism class and function

Time:01-07

i am start to learn how to write python code

There is an option to write code ones onthis situation? i want to crate class and 2 class how extend from her and i want to check if i can loop on only ones my example:

class animal:
    def printDetail(self):
       print(self.name)

class bird(animal):
    def printDetail(self):
       super(bird, self).printName()
       print(self.wingsSize)

class fish(animal):
    def printDetail(self):
       super(fish, self).printName()
       print(self.weight)

fishList = []
birdList = []
animalList = []

def main():
     for a in (animalList,fishList,birdList):
          a.printDetail()

main()

when i try to do it i got an error that AttributeError: 'list' object has no attribute 'printDetail' like this is an unknow function. i understand that it try to take the attribute of the list class but there is any option that i can do it more esear then:

     for a in animalList:
          a.printDetail()
     for a in fishList:
          a.printDetail()
     for a in birdList:
          a.printDetail()

that is work fine but to long?

CodePudding user response:

The first code snippet creates a 3-tuple of lists. You're invoking .printDetail() on every list in that tuple.

To create a list that contains the elements from each list (as opposed to a list that contains the lists themselves), you can use for a in (animalList fishList birdList):

CodePudding user response:

Here are some possibilities:
nested loops:

for lst in (animalList, fishList, birdList):
    for a in lst:
        a.printDetail()

concatenate lists:

for a in animalList   fishList   birdList:
    a.printDetail()

unpacking:

for a in (*animalList, *fishList, *birdList):
    a.printDetail()

CodePudding user response:

As others have already answered, there are a variety of quick ways to do this. I prefer the unpacking method that Wups uses in his answer.

However, I also wanted to check if we needed to add initializations to each of these classes in order for the print to work. Further, I was thinking when you called printName in some methods, you meant printDetail instead (maybe I am wrong?). Thus I also revised the class code also, I hope it may benefit you and others who may want to run the code and see a result:

class animal:
    def __init__(self, name):
        self.name=name
    def printDetail(self):
       print(self.name)

class bird(animal):
    def __init__(self, name, wingsSize):
        self.name=name
        self.wingsSize = wingsSize
    def printDetail(self):
       super(bird, self).printDetail()
       print(self.wingsSize)

class fish(animal):
    def __init__(self, name, weight):
        self.name=name
        self.weight=weight
    def printDetail(self):
       super(fish, self).printDetail()
       print(self.weight)

fishList = [fish("salmon",12)]
birdList = [bird("eagle",4)]
animalList = [animal("bear")]

def main():
     for a in (*animalList, *birdList, *fishList):
          a.printDetail()

main()

Output:

bear
eagle
4
salmon
12
  •  Tags:  
  • Related