Created
July 22, 2022 08:57
-
-
Save petrsvihlik/c98e3b66d6ccb4287a4ac93ffb363ef6 to your computer and use it in GitHub Desktop.
Demonstration of all kinds of required and optional arguments in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# positional, keyword or positional args - https://www.educative.io/answers/what-are-positional-only-arguments-in-python | |
# keyword-only args - https://peps.python.org/pep-3102/ | |
# kwargs - https://www.w3schools.com/python/gloss_python_function_arbitrary_keyword_arguments.asp | |
from typing import Optional | |
"""two | |
Function that demonstrates all kinds of required and optional arguments. | |
:keyword kwarg_optional: optional kwarg | |
""" | |
def all_types_of_arguments( | |
positional_required, | |
positional_optional, # this could be made optional by adding =None | |
/, | |
keyword__or_positional_required, | |
keyword__or_positional_optional=None, | |
*args, | |
kw_only_required, | |
kw_only_optional: Optional[int] = None, | |
**kwargs, | |
): | |
print( | |
positional_required, | |
positional_optional, | |
keyword__or_positional_required, | |
keyword__or_positional_optional, | |
*args, | |
kw_only_required, | |
kw_only_optional, | |
kwargs.pop("kwarg_optional", None), | |
) | |
all_types_of_arguments(1, 2, 3, kw_only_required=8) | |
all_types_of_arguments(1, 2, 3, 4, 5, 6, 7, kw_only_required=8, kw_only_optional=9, kwarg_optional=10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment