0

This is a syntax I see in Python3 code skeletons on leetcode.com. Note the type declarations of the function input arguments (or at least I think they're type declarations, never seen it before). nums must be a List of int, and s must be an int.

class Solution:
    def findTargetSumWays(self, nums: List[int], S: int) -> int:
        pass

If I run that function inside the environment on leetcode.com, it exits without error. However, if I run the same code in my own Python 3.7.3 environment, I get a NameError.

def findTargetSumWays(self, nums: List[int], S: int) -> int: NameError: name 'List' is not defined

What is wrong? Is the syntax on leetcode.com even real Python?

Magnus
  • 589
  • 8
  • 26

1 Answers1

1

Yes, this is real syntax, officially used since 3.7 I believe.

The List used in this static typing is imported from the typing module.

You can read more about it in PEP-484: Type Hints or in the typing module docs.

Faboor
  • 1,365
  • 2
  • 10
  • 23