[Tutor] 42
Christian Wyglendowski
Christian.Wyglendowski at greenville.edu
Fri Jun 11 11:51:46 EDT 2004
> -----Original Message-----
> 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;
> }
>
Don't know how convincing it is, but here is my idea:
class define:
... def __init__(self, strnumexpr):
... self.val = strnumexpr
... def __mul__(self, x):
... answer = self.val + '*' + x.val
... return eval(answer)
... def __repr__(self):
... return str(eval(self.val))
...
>>> SIX = define('1 + 5')
>>> NINE = define('8 + 1')
>>> SIX
6
>>> NINE
9
>>> SIX * NINE
42
You could hide the define class in a module which might make it more
convincing...
>>> from hhgtg import define
>>> SIX = define('1 + 5')
>>> NINE = define('8 + 1')
>>> SIX
6
>>> NINE
9
>>> SIX * NINE
42
Christian
http://www.dowski.com
More information about the Tutor
mailing list