You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 22:15:39