python - how to apply decorators to Python functio

2019-08-12 05:53发布

Prefer a micropython answer but will accept CPython

I'm implementing a Python function in C.

How do you apply a decorator to Python function written in C?

1条回答
时光不老,我们不散
2楼-- · 2019-08-12 06:29

Decorators can be invoked with a function as their argument. So if you would have written this (in Python):

@mydeco
def myfunc(x, y):
    return x * y

You can instead write this:

def myimpl(x, y):
    return x * y

myfunc = mydeco(myimpl)

You can then move myimpl to C.

If your decorator takes arguments:

myfunc = mydeco(a, b)(myimpl)
查看更多
登录 后发表回答