[英]How to call a method of an object returned from an async function without parentheses in Python?
I got this function:我得到了这个 function:
async def download(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if resp.status == 200:
return resp
To get the data, I'd need to write:要获取数据,我需要编写:
text = await (await utils.download(url)).text()
How to change download
so I can write like this如何更改
download
以便我可以这样写
text = await utils.download(url).text()
without getting AttributeError: 'coroutine' object has no attribute 'text'
?没有得到
AttributeError: 'coroutine' object has no attribute 'text'
吗?
May be this can help simplifying the usage:可能这有助于简化使用:
async def download(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if resp.status == 200:
return await resp.text()
Then to use it:然后使用它:
async def main():
text = await download("http://python.org")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.