Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

python - How do I get a unique hash for the fully inlined code of a function?

My idea is to build some kind of caching mechanism for functions. To that end, I need to determine if a function needs to be evaluated. The result of a function depends - for the sake of this example - on its parameters and the actual code of the function. There may be calls to other functions inside the function. Therefore, only the fully inlined code of the function is a useful "value" for determining whether a function needs to be reevaluated.

Is there a good way to get this fully inlined code of a function in python?

question from:https://stackoverflow.com/questions/65648743/how-do-i-get-a-unique-hash-for-the-fully-inlined-code-of-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not possible. The "fully inlined code" of a Python function isn't a well-defined concept, for multiple reasons.

First, almost anything a Python function refers to can be redefined at runtime, which invalidates ahead-of-time inlining. You can even replace built-ins like print.

Second, even with no such rebinding, it is impossible to "fully inline" a recursive or indirectly recursive function.

Third, a function can and almost always will invoke code dynamically based on the provided parameters. def f(x): return x.something() requires a concrete value of x to determine what something is. Even something like def f(x, y): return x + y dynamically invokes an __add__ or __radd__ callback that can't be determined until the actual values of x and y are known.

Fourth, Python functions can invoke C code, which cannot be inlined at Python level.

Finally, even if all the problems with "fully inlining" a function didn't exist, a "fully inlined" version of a function still wouldn't be enough. There is no general way to determine if a Python function performs state mutation, or depends on mutable state that has been mutated, both of which are major issues for caching.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...