Last active
September 26, 2024 09:49
-
-
Save robinvandernoord/c055f0463f1fb0fef4b5cc853d6ee201 to your computer and use it in GitHub Desktop.
Python `dbg!()`
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
import typing as t | |
# Define a single type variable | |
T = t.TypeVar("T") | |
Ts = t.TypeVarTuple("Ts") | |
@t.overload | |
def dbg(arg: T, **print_settings: t.Any) -> T: | |
""" | |
If you do dbg on one item, it will return that item. | |
""" | |
@t.overload | |
def dbg(*args: t.Unpack[Ts], **print_settings: t.Any) -> tuple[t.Unpack[Ts]]: | |
""" | |
If you do dbg on multiple items, it will return the tuple of those items. | |
""" | |
def dbg(*args, **print_settings): | |
print(*args, **print_settings) | |
if len(args) == 1: | |
return args[0] | |
else: | |
return args | |
if __name__ == '__main__': | |
abc = dbg("abc") | |
one, two = dbg(1, 2) | |
print(f"{abc = }; {one = }; {two = }") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment