Python Concepts

Python Concepts


 

The Python concept of passing “nums: List[int]” to python function

TL;DR

nums: List[int] in Python is a type hint that suggests a function parameter should be a list of integers. It improves code readability and allows for static type checking, but doesn't enforce types at runtime. This syntax is part of Python's type annotation system, which helps developers catch potential errors early and write more maintainable code.
 
 

Explanation

The syntax nums: List[int] in Python is called a type hint or type annotation. It is a way to indicate the expected type of the nums variable in a function or a method. This helps make the code more understandable and maintainable, but it doesn't enforce any type checks at runtime. Python remains dynamically typed, meaning it doesn't require explicit type declarations and won't raise an error if the type isn't correct unless you explicitly check it.
 
Let's break down the syntax nums: List[int]:
 

1. nums :

 
  • This is the name of the parameter or variable. In this case, it refers to the list that the function is expecting to receive.
 

2. : (Colon):

 
  • The colon is used to indicate that what follows is the type annotation for the variable nums.
 

3. List[int] :

 
  • This is the type hint for nums, indicating that it is expected to be a list of integers.
  • List is a generic type provided by Python’s typing module, which allows you to specify the type of elements contained in the list. In this case, List[int] means that nums should be a list where all the elements are of type int.
 

Example:

 
from typing import List def sum_numbers(nums: List[int]) -> int: return sum(nums)
 
 
  • Function Name: sum_numbers
  • Parameter: nums, which is annotated with List[int], meaning it should be a list of integers.
  • Return Type: > int indicates that the function will return an integer.
 

Usage:

 
# Valid: all elements are integers result = sum_numbers([1, 2, 3, 4]) print(result) # Output: 10 # No error at runtime, but the type hint suggests this is incorrect result = sum_numbers(["a", "b", "c"])
 

Important Points:

 
  1. Type hints are not enforced: Python does not enforce the types at runtime. Even if you pass something other than a list of integers, the program will not throw a type error unless the wrong type causes an operation to fail.
For example:
result = sum_numbers([1, 2, "3", 4]) # No error until runtime
  • In this case, it will throw a runtime error when trying to sum the elements, as the string "3" can't be added to integers.
  1. Use the typing module: List[int] requires you to import List from the typing module, which allows for more complex type hints like List, Dict, Tuple, etc.
  1. Code Readability and Tools:
  • Type hints help make the code more understandable, especially in larger projects, by making it clear what kind of data structures are expected.
  • Static type checkers like mypy can be used to check type hints during development, helping to catch potential type errors before runtime.
 

Full Example with Return Type:

from typing import List def multiply_numbers(nums: List[int]) -> int: result = 1 for num in nums: result *= num return result
 
  • Here, nums: List[int] indicates that nums should be a list of integers.
  • > int indicates that the function will return an integer.
 

Conclusion:

nums: List[int] is a type annotation that indicates nums is expected to be a list where each element is an integer. This makes your code clearer and allows for better error checking with tools like mypy, but it doesn't enforce any rules at runtime.