如何解决Python中的SyntaxError: positional argument follows keyword argument错误?
电报机器人代码语法错误排查
报错信息
) for links in link] ^ SyntaxError: positional argument follows keyword argument
错误原因
- 创建
types.InlineQueryResultArticle实例时,id参数写法有误:原本应该是完整的hashlib.md5(f'{link["id"]}'.encode()).hexdigest(),但错误地把.hexdigest()拆成独立的位置参数,放在了id = ...这个关键字参数之后,违反了Python参数传递规则——位置参数不能出现在关键字参数的后面。
修正后的代码
@dp.inline_handler() async def inline_handler(query : types.InlineQuery): text = query.query or "echo" links = searcher(text) articles = [types.InlineQueryResultArticle( id = hashlib.md5(f'{link["id"]}'.encode()).hexdigest(), title = f'{link["title"]}', url = f'https://www.youtube.com/watch?v={link["id"]}', thumb_url = f'{link["thumbnails"][0]}', input_message_content=types.InputTextMessageContent( message_text=f'https://www.youtube.com/watch?v={link["id"]}') ) for link in links] await query.answer(articles, cache_time=60, is_personal=True)
内容的提问来源于stack exchange,提问作者Ros Unreal
相关产品推荐
相关产品推荐

