Search code examples
python-3.xfunctionspyderfunction-declaration

Function Declaration using specific datatypes


def StrongestNeighbour(neighbours: list[int])-> list[int]:

this kind of function declaration is only working in my vs code but not in spyder what is the problem.


Solution

  • Probably with Spyder you are running Python version <3.9+ and with VSCode is Python >=3.9+

    The thing is that before Python 3.9, for type hints like the one you show, the syntax is a little bit different:

    from typing import List
    
    def strongestNeighbour(neighbours: List[int]) -> List[int]:
        return neighbours
    
    strongestNeighbour([1,2,3,4])
    

    Note the import from the typing module and the uppercase L.

    You can check the Python 3.8 typing docs for more information on the import and the overall syntax: https://docs.python.org/3.8/library/typing.html

    However, if you use Python 3.9 then you can use the list[int] syntax as you have it. You can check the Python 3.9 typing docs for more information: https://docs.python.org/3.9/library/typing.html