1

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?

2
  • 1
    You can put everything into one list (for example, by joining the lists together); or you can use two loops - one to loop over (animalList,fishList,birdList) and the second to loop over the list that you get from that. Commented Jan 7, 2022 at 0:05
  • Just a side note to help with your learning. It is standard in python (see the python style guide at python.org/dev/peps/pep-0008) to name classes with CapitalCase, so your classes would be named Animal, Bird, and Fish. I know that's a lot to think about when you are first getting started, but getting used to the standard will help a lot as you read other code, and help you develop consistency for yourself. Good luck with your Python learning! Commented Jan 7, 2022 at 0:42

2 Answers 2

2

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):

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

Comments

0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.