I think @
buran explanation is good,some addition info that use type hints.
For the user of function it's most important what should be passed in,
not how the creator of function handle arguments from tuple(*args) or dict(**kwargs).
Type annotations give a hint of what's wanted in as arguments in without any documentation(as it should also be).
def foo(*boo: int, **hoo: str):
print(sum(boo), type(boo))
print(hoo, type(hoo))
foo(1, 2, 3, 4)
print('-'*15)
foo(a='car', b='Taxi')
Output:
10 <class 'tuple'>
{} <class 'dict'>
---------------
0 <class 'tuple'>
{'a': 'car', 'b': 'Taxi'} <class 'dict'>
Type hints by themselves don’t cause Python to enforce types,so will give error(TypeError) in sum() if give a string.
def foo(*boo: int, **hoo: str):
print(sum(boo), type(boo))
print(hoo, type(hoo))
foo('car', 2, 99)
Output:
Traceback (most recent call last):
File "code_type1.py", line 5, in <module>
foo('car', 2, 99)
File "code_type1.py", line 2, in foo
print(sum(boo), type(boo))
TypeError: unsupported operand type(s) for +: 'int' and 'str
Have to use a static type checker like
Mypy,to catch error earlier.
Output:
E:\div_code
λ mypy code_type1.py
code_type1.py:5: error: Argument 1 to "foo" has incompatible type "str"; expected "int"
Found 1 error in 1 file (checked 1 source file)