Skip to content

Commit b15b3fd

Browse files
authored
Merge pull request #80 from braboj/codex/add-header-for-handling--args-and---kwargs-in-python-2
Add Python2 and Python3 variable argument comparison
2 parents d05478d + 2f83baf commit b15b3fd

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

examples/07_functions/func_variable_arguments_python2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Handling a variable number of arguments in Python 2
2+
# -----------------------------------------------------------------------------
3+
# Demonstrates capturing extra positional arguments with `*args` and
4+
# extra keyword arguments with `**kwargs` when keyword-only parameters are
5+
# unavailable. In Python 3 you can declare keyword-only parameters using
6+
# the `*` separator instead of relying on `**kwargs`. See
7+
# `func_variable_arguments_python3.py` for comparison.
8+
19
def variable_number_of_arguments(a, b, *args, **kwargs):
210
print("a: {a}".format(a=a))
311
print("b: {b}".format(b=b))

examples/07_functions/func_variable_arguments_python3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Handling a variable number of arguments in Python 3
2+
# -----------------------------------------------------------------------------
3+
# Keyword-only arguments come after `*` so there is less need to use
4+
# `**kwargs` for optional named parameters. Compare with the Python 2 version
5+
# in `func_variable_arguments_python2.py`.
6+
17
def variable_number_of_arguments(a, *args, b=1, **kwargs):
28
print(f"a: {a}")
39
print(f"b: {b}")

0 commit comments

Comments
 (0)