python有宏吗?

时间:2015-05-06 15:14:55

标签: python 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调用替换为os.path.dirname(os.path.realpath(__file__))内的app.py。因为否则返回的目录将是lib.py所在的目录,其不一定与app.py所在的目录相同。

2 个答案:

答案 0 :(得分:0)

不,Python中没有宏。我看到的唯一干净的解决方案是将参数传递给您的函数。

答案 1 :(得分:-2)

排序。查看eval()函数和/或exec语句。