'Python conflict with keyword "from" in telegram bot

I would like to print user information in Python script using python-telegram-bot referring to this page

But when I type

print update.message.from

keyword .from is recognized as internal reserved keyword and I get invalid syntax error.

How can I solve?

Thanks.



Solution 1:[1]

As explained in the python-telegram-bot code and documentation itself:

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

So: print update.message.from_user.

Solution 2:[2]

Use getattr.

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

It's not the name of the key that is a problem, but the attempt to access it using attribute syntax.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kelly Bundy
Solution 2