filmov
tv
Optional Arguments in Python With *args and **kwargs

Показать описание
An overview on Python's *args and **kwargs feature for writing functions with a variable number of arguments and/or making some parameters that a function accepts optional.
In this tutorial I also demonstrate real world applications and use cases for optional parameters.
For example, a design pattern you can use to extend the behavior of a parent class without having to replicate the full signature of its constructor in the child class.
In summary, this is how optional function and method parameters work in Python:
*args and **kwargs let you write functions with a variable number of arguments in Python.
*args collects extra positional arguments as a tuple. **kwargs collects the extra keyword arguments as a dictionary.
The actual syntax is * and **. Calling them args and kwargs is just a convention (one you should stick to).
To learn how to use the full potential of Python check out "Python Tricks: The Book" at the link below.
* * *
FREE Python Tutorials & News:
Комментарии