[Tutor] 42

Roeland Rengelink roeland.rengelink at chello.nl
Fri Jun 11 03:10:59 EDT 2004


Gregor Lingl wrote:

> What is the most convincing way to mimick this in Python?
>
> #include <stdio.h>
>
> #define SIX 1 + 5
> #define NINE 8 + 1
>
> int main(void) {
>  printf("What do you get if you multiply six by nine: %d\n", SIX *
>     NINE);
>  return 0;
> }

Since macro's are substituted before compilation in C. You'd have to 
substitute before iterpretation in Python. This works:

 >>> SIX = '1+5'
 >>> NINE = '8+1'
 >>> eval('%s*%s' % (SIX, NINE))
42


Have fun,

Roeland



More information about the Tutor mailing list