'Python Meta Programming
I'm currently working on a rather big API project written in Python. I have a couple of endpoints, in each of which I want to perform more or less the same snippet of code. However, I don't want to copy paste it, and it is rather complicated to put it into an extra function as there are always a couple of context variables like loggers or requests. Is there some sort of meta programming mechanism in Python, that lets me "expand" / dynamically "write" code on runtime / interpretation time? I'm used to this from closure, so maybe some of you know what I'm talking about.
Here is an example:
# code I want in each function that uses x, y and local logger
def f(x, y):
# reference to the above code
# actual code of f
def g(x, y, z):
# reference to the above code
# actual code of g
I tried to use a decorator, but that does not seem clean to me, and it interferes with other decorators, if used like I did.
As a note, I'm writing purely sequential / functional code.
Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
