Last active
August 5, 2024 17:58
-
-
Save CodeByAidan/cd8bffdb34891d6216a7956d50165a40 to your computer and use it in GitHub Desktop.
A test for special parameters in Python 3.12 + explanation for beginners
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
def baz( | |
a: int, | |
b: int, | |
/, | |
c: int | None = None, | |
*args: int, | |
d: int | None = None, | |
**kwargs: int, | |
) -> None: | |
print("--------------------") | |
print(f"a: {a}, b: {b}") | |
print(f"c: {c}") | |
print(f"args: {args}") | |
print(f"d: {d}") | |
print(f"kwargs: {kwargs}") | |
baz(1, 2) # Minimal call with position-only arguments | |
baz(1, 2, 3, 4, 5) # Adding more positional arguments | |
baz(1, 2, c=3, d=4, e=5) # Using keyword arguments for c, d, and an extra one | |
baz(1, 2, 3, 4, d=5, e=6) # Mixing positional and keyword arguments |
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
-------------------- | |
a: 1, b: 2 | |
c: None | |
args: () | |
d: None | |
kwargs: {} | |
-------------------- | |
a: 1, b: 2 | |
c: 3 | |
args: (4, 5) | |
d: None | |
kwargs: {} | |
-------------------- | |
a: 1, b: 2 | |
c: 3 | |
args: () | |
d: 4 | |
kwargs: {'e': 5} | |
-------------------- | |
a: 1, b: 2 | |
c: 3 | |
args: (4,) | |
d: 5 | |
kwargs: {'e': 6} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Analysis:
baz(1, 2)
a
andb
are provided, with default values for the restbaz(1, 2, 3, 4, 5)
c
is specified by position, andargs
captures the additional positional arguments4
and5
.baz(1, 2, c=3, d=4, e=5)
c
andd
are specified by keyword, ande
is stored inkwargs
.baz(1, 2, 3, 4, d=5, e=6)
c
is specified by position,args
captures4
, andd
ande
are ind
andkwargs
, respectively