문제

I want to get the original file/scriptname, etc of the function that is being decorated. How can I do that?

   def decorate(fn):
        def wrapped():
            return "scriptname: " + fn.scriptname?  
        return wrapped

I tried using fn.__code__ but that gave me more stuff that I needed. I could parse that string to get the function name, but was wondering if there is a more elegant way to do it

도움이 되었습니까?

해결책

Try this:

return "filename: " + fn.func_code.co_filename

다른 팁

import inspect
inspect.getfile(fn)

This won't work for builtin functions though, you have to fall back to inspect.getmodule for those.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top