Python 与电报机器人中的关键字 "from" 冲突

Python conflict with keyword "from" in telegram bot

我想使用 python-telegram-bot 参考 this 页面

在 Python 脚本中打印用户信息

但是当我输入

print update.message.from

关键字 .from 被识别为内部保留关键字,我收到无效语法错误。

我该如何解决?

谢谢。

python-telegram-bot code and documentation 本身所述:

  • In Python from is a reserved word, use from_user instead.

所以:print update.message.from_user.

使用getattr.

x = getattr(update.message, 'from').id

问题不在于密钥的名称,而是尝试使用属性语法访问它。