0

I was given python 3.x code but my python 2.7 interpreter won't interpret it due to the colons in the function definition.

The code I run:

def __init__(self, api_key: str, base_url: Tuple[str, None] = None):

And the error I get:

File "C:\Users\3791108\Downloads\CloudClient.py", line 191
        def __init__(self, api_key: str, base_url: Tuple[str, None] = None):

                                  ^
SyntaxError: unexpected token ':'
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
David
  • 1
  • 5
  • 1
    Wouldn't say it's a duplicate. This question asks `what`, the question in the link asks `why`... – Tomerikoo Jul 02 '19 at 18:33
  • 1
    Here's a question that asks `what`: https://stackoverflow.com/questions/41648300/what-does-the-colon-inside-the-parameter-mean – Thomas Jul 02 '19 at 18:34

1 Answers1

1

The colons are type declarations.

Python 3 now has optional type support. The function definition is saying that api_key should be a string, and that base_url should be a tuple where the first element is a string and the second a NoneType.

I think the real question is why are you trying to run python 3.X code in a 2.7 interpreter? This is a different language with no guarantee of compatibility as you have found.

Thomas
  • 871
  • 2
  • 8
  • 21
  • I was given an api client file for python. The application that will be the client uses ironpython 2.7 as an intepreter. Thus, I am tweaking the 3.x python file to run through an ironpython 2.7 intepreter. Thanks for your help – David Jul 02 '19 at 18:42