【发布时间】:2020-03-02 23:21:27
【问题描述】:
如何循环遍历对象列表并调用它们的函数。例如:
class Cat:
def talk():
print("Meow")
class Dog:
def talk():
print("Woof")
cat = Cat()
dog = Dog()
animal_list = [cat, dog]
# How would I do these async?
for animal in animal_list:
animal.talk()
此线程 How to use an async for loop to iterate over a list? 建议使用 asyncio,但没有举例说明如何让对象调用它自己的函数,例如 animal.talk()
【问题讨论】:
-
您可以将
talk函数设为异步函数吗?这样更容易。 -
是的,看起来怎么样?
标签: python asynchronous python-asyncio