Skip to content

Instantly share code, notes, and snippets.

@robinvandernoord
Last active September 26, 2024 09:49
Show Gist options
  • Save robinvandernoord/c055f0463f1fb0fef4b5cc853d6ee201 to your computer and use it in GitHub Desktop.
Save robinvandernoord/c055f0463f1fb0fef4b5cc853d6ee201 to your computer and use it in GitHub Desktop.
Python `dbg!()`
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