#define Python 的模板

Templates that #define for Python

我应该使用众多模板框架中的哪一个才能在 Python 中使用像这样的简单宏?基本的宏似乎就是我所需要的,而且可用的框架数量太多了。

#define R(a,b,c,k) as b ^= (a + c) << k
R(x[ 0], x[ 4], x[12], 7)
R(x[ 5], x[ 9], x[ 1], 7)

它会解析成这样的东西(数学代码):

x[ 4] ⊕= (x[ 0] ⊞ x[12])<<<7;    
x[ 9] ⊕= (x[ 5] ⊞ x[ 1])<<<7;

我按照 Simeon 的建议研究了 Jinja2。它看起来很方便,而且还有宏! http://jinja.pocoo.org/docs/dev/templates/#macros