python 有宏吗?
Does python have macros?
我想知道 python 是否有像 C/C++ 这样的宏,它会在调用方站点插入代码而不是进行函数调用。我想到的一个用例如下
# In lib.py
def get_current_dir():
return os.path.dirname(os.path.realpath(__file__))
# In app.py
current_dir = get_current_dir()
这不起作用,除非 get_current_dir
是一个宏,即它将 get_current_dir
调用替换为 app.py
内的 os.path.dirname(os.path.realpath(__file__))
调用。因为否则返回的目录将是 lib.py
所在的目录,不一定与 app.py
所在的目录相同。
我想知道 python 是否有像 C/C++ 这样的宏,它会在调用方站点插入代码而不是进行函数调用。我想到的一个用例如下
# In lib.py
def get_current_dir():
return os.path.dirname(os.path.realpath(__file__))
# In app.py
current_dir = get_current_dir()
这不起作用,除非 get_current_dir
是一个宏,即它将 get_current_dir
调用替换为 app.py
内的 os.path.dirname(os.path.realpath(__file__))
调用。因为否则返回的目录将是 lib.py
所在的目录,不一定与 app.py
所在的目录相同。