decorator involving assigning `staticmethod` doesn't work as expected?

Viewed 20

I have two decorators, one involves assigning a staticmethod and the other one doesn't:

def mark_statistics(func):
    new_func = staticmethod(func)
    new_func.is_stats_mark = True
    return new_func


def log_args(func):
    def wrapper(*args, **kwargs):
        print(f"""args are: {args}
        kwargs are: {kwargs}""")
        return func(*args, **kwargs)

    wrapper.is_logging_mark = True

    return wrapper


class Utility:
    @mark_statistics
    @log_args
    def make_to_static(*args, **kwargs):
        #... some processing ...
        result = "result from <make_to_static>"
        return result

    @log_args
    def normal_meth(self):
        result = "result from <normal_meth>"
        return result


u = Utility()
u.make_to_static.is_logging_mark
u.make_to_static.is_stats_mark  # failed

Running this code produce the following error:

AttributeError: 'function' object has no attribute 'is_stats_mark'

As you can see, the first mark, is_logging_mark can run successfully, but not the case in the second mark, is_stats_mark. For some reason, that mark didn't get attached onto the decorated func.

I've been scratching my head trying to figure out why? Someone please give some hint? Thanks.

0 Answers
Related