简体   繁体   English

Python 3 Asyncio协程相等

[英]Python 3 Asyncio coroutine equality

class A:
  async def func2(self):
    pass

In a unit test I want to assert that a certain coroutine gets passed with an equality check ie 在单元测试中,我想断言某个协程通过相等性检查即通过

arg1 == func2()

However, couroutines have an address associated with them: 但是,couroutines具有与之关联的地址:

'<coroutine object Class.func1 at 0x12341234>'

Is there a canonical way to test coroutine equality? 有没有规范的方法可以测试协程平等? Should I just regex the __str__ representation of the coroutine? 我应该只对协程的__str__表示形式进行正则表达式吗?

You can get a bit better than comparing __repr__ s -- you can compare the underlying code object: 与比较__repr__相比,您可以得到更好的结果–您可以比较基础代码对象:

class A:
    async def func2(self):
        pass

a = A()
x = a.func2()
y = a.func2()

print(x == y)                   # False
print(x.cr_code is y.cr_code)   # True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM