python - 如何在可等待的子类中使用 super()?
问题描述
我想通过子类化现有的等待类来为它添加一个新功能。
让我们从一个非常简单的基类开始,它创建在短暂睡眠后异步返回 99 的对象。子类应该只在结果中添加 +1。
我找不到super()
引用基类的正确方法。
import asyncio
class R99:
def __await__(self):
loop = asyncio.get_event_loop()
fut = loop.create_future()
loop.call_later(0.5, fut.set_result, 99)
return fut.__await__()
class R100(R99):
async def add1(self):
v = await R99()
#v = await super().__await__() # <== error
return v + 1
def __await__(self):
return self.add1().__await__()
async def test():
print(await R99())
print(await R100())
asyncio.get_event_loop().run_until_complete(test())
解决方案
推荐阅读
- swift - 具有泛型和专业化的协议
- matlab - 寻找具有不同采样频率的方波的周期
- python-3.x - Python中使用对象的点表示法
- c# - 当应用程序运行多个线程时,Owin selffhost Web 服务器响应缓慢
- angular - 无法解析 AdminProductsComponent 的所有参数
- excel - 如何使用 Application.VLookup 防止数据丢失?
- regex - 使用正则表达式解析 Cisco 系统日志
- python - 在一个端点 django rest 框架中使用两个不同的序列化程序
- typescript - 如何从 TypeScript 联合类型中获取一组字符串文字?
- sql - 我有两个表部门和员工,我想获得员工技能不等于程序员但它不起作用的部门名称