You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's probably a better name for this, but the idea is to write decorator + kwargs -- so you don't have to do @decorator() if there aren't any arguments being passed.
defmaybe_arg_decorator(callable):
""" There's this fun trick that dataclasses.dataclass decorator does, where you can use it both directly as a decorator, or call it with kwargs and use that as the decorator. This helper converts a function with 1 positional argument and a bunch of kwargs into one of those. @maybe_arg_decorator def simple_decorator(decorated, a=1, b=2): pass @simple_decorator def thing(): pass @simple_decorator(a=3) def other_thing(): pass """@wraps(callable)defwrapper(decorated=None, /, **kwargs):
ifdecorated:
returncallable(decorated)
defdouble_wrapper(decorated, /):
returncallable(decorated, **kwargs)
returndouble_wrapperreturnwrapper
The text was updated successfully, but these errors were encountered:
There's probably a better name for this, but the idea is to write decorator + kwargs -- so you don't have to do
@decorator()
if there aren't any arguments being passed.The text was updated successfully, but these errors were encountered: