简体   繁体   中英

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:

'<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?

You can get a bit better than comparing __repr__ s -- you can compare the underlying code object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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