11.1 Infinite Number of Parameters
In Python, functions can be designed to accept an infinite
number of parameters. This is achieved using arguments passed
via *args
for regular parameters and **kwargs
for
named parameters.
Using *args
*args
allows a function to accept an arbitrary number
of positional arguments, which the function treats as a tuple.
Here's an example:
def print_all(*args):
for item in args:
print(item)
print_all(1, 'apple', True) # will output 1, apple, and True.
The variable args
will contain a tuple of all
the parameters that were passed to the function when it was called.
Using **kwargs
**kwargs
works similarly to *args
, but for
named arguments, represented as a dictionary. This allows
the function to accept any number of named arguments:
def print_named_items(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_named_items(fruit='apple', number=1) # will output fruit: apple and number: 1
These mechanisms make functions extremely flexible and useful in scenarios where the number of parameters is unknown in advance, such as in API development or event handling functions.
We'll explore both cases in more detail later, but you should know that in Python, you can easily access all the arguments passed to a function. And you can pass anything you want to the function.
11.2 Specifying Types
As you know, variables in Python don't have a predetermined type. You can assign a value of any type to any variable, and in the next line assign a new type, and so on.
This is very convenient for small programs and not so convenient for large ones. When a program is very large and the functions amount to thousands, a programmer can't remember exactly what data needs to be passed to a function and in what order.
In languages with static typing, the IDE itself hints to programmers what parameters and types need to be passed when calling a function. But Python – it's still a dynamically typed language, so there's nowhere for such hints to come from.
That's why the creators of Python came up with something known as
type hinting
. It's kind of like variable types, but they're more
recommendations than strict types. The specified types don't impose any restrictions.
Type hinting was introduced in Python 3.5 and became popular thanks to PEP 484. It's a mechanism that allows programmers to specify expected types for variables, arguments, and function return values.
Even though Python remains dynamically typed, type hinting adds clarity and supports static type checking. Here's an example of type hinting:
def add_numbers(a: int, b: int) -> int:
return a + b
The variable type is indicated with a colon, and the type of the function's return result with an arrow.
Benefits provided by type recommendations:
- Improved Documentation: Explicit type hints make the code self-documenting.
- Better Autocompletion: Code editors can use type information to improve autocompletion.
- Debugging Aid: Static type checking can identify potential errors before the program runs.
Tools for Working with Types
Python itself doesn't react to declared variable types. Instead, Python has a special utility — mypy — a popular static type checker that helps catch errors in code using type hints.
GO TO FULL VERSION