How to make a decorator that can be used WITH or without decorator parameters, but keep the same behavior

Trying to normalize behavior between a decorator WITH and without
parameters, it’s not going well…
There appears to be 2 problems with this.

  1. with decorator parameter’ it gets called immediately,
    but without it gets called at use time. Ideally it would be
    processed immediately resulting in the function being
    replaced with it’s Callback instance.
  2. I can’t figure out in this context of immediate verses late
    execution, how to normalize func as the decorated func, and
    cls as the Callback bases class.
    Or perhaps there is a better way to do this. Thinking on the
    property setters which use a prefix in the name, instead of as
    parameter. I how this illustrates what I am trying to accomplish,
    if unclear please comment your doubts and queries. Thank you.

The sample code is below or can be found at https://gist.github.com/ismaelharunid/0d3a5510149bc120537c1914bad73b08

# ./callback_decorator.py

from collections.abc import Callable
from types import FunctionType
#from functools import partial, wraps


class Callback(Callable):
    def __call__(self, *pargs, **nargs):
        print(type(self).__name__, pargs, nargs)


class CustomCallback(Callback):
    pass


def callback(target_or_class, base=Callback):
    "decorator for normalizing callbacks."

    def a_function_that_returns_the_target():
        def dummy(*pargs, **nargs):
            print("dummy", pargs, nargs)
        return dummy

    def wrapper():
        def normalize():
            """
            Code to resolve the following values:
                func:FunctionType  The target function to decorate;
                cls:type  a Callback class type as the interface.
            as target_or_class' contents will vary depending on whether
            the decorator was called (with decorator parameters) or simply
            decorating (without decorator parameters). Examples below
            decorator.
            """
            if isinstance(target_or_class, type):
                cls = target_or_class
                if not issubclass(cls, base):
                    raise ValueError("`cls` expects a subclass of {!r},"
                                     " but found {!r}".format(base, cls))
                func = a_function_that_returns_the_target()
            elif isinstance(target_or_class, FunctionType):
                func = target_or_class
                cls = base
            else:
                raise ValueError("parameter expects a class,"
                                 " but found {!r}"
                                 .format(target_or_class))
            return func, cls
        func, cls = normalize()
        name = ''.join(token for token
                       in (i.strip() for i
                           in func.__name__.title().split('_'))
                       if token) + cls.__name__
        return type(name, (cls,), dict(__call__=func))()
    return wrapper()


@callback   # note, without parameters
def callback1(self, *pargs, **nargs):
    print("called callback1", self, pargs, nargs)

callback1()

@callback(CustomCallback)   # note, WITH parameters
def callback2(self, *pargs, **nargs):
    print("called callback2", self, pargs, nargs)


callback2()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật