How to write differently to remove this type hint in Python 2.7?

Chris Angelico rosuav at gmail.com
Wed Oct 21 17:28:25 EDT 2020


On Thu, Oct 22, 2020 at 8:26 AM Shaozhong SHI <shishaozhong at gmail.com> wrote:
>
> Is there another way to do this?
>
> def greet(name: str) -> str:
>     return "Hello, " + name
> greet
>
> File "<ipython-input-28-bee2cddf1f0d>", line 1 def greet(name: str) -> str:
> ^ SyntaxError: invalid syntax

If you need to support Python 2, don't use annotations. It's that simple. :)

Bear in mind that Python 2 is now extremely old, and is no longer
supported. A much better option is to use a newer interpreter.

ChrisA


More information about the Python-list mailing list