Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Well actually Python does have an eval() that can be called on expressions in the form of strings. The problem is that any potential macro anything would be nowhere near as seamless as it in Lisp, since the parse tree isn't right there for everything. But with enough effort (and closing your eyes to avoid how horrible it would look), you could maybe make something.


I'm not sure. I think scoping would screw you up. First off, there's a small reason right away why it wouldn't work. You'd have to quote your argument to debugExpr, as in, say, debugExpr('2 times 3'). I did a little test at the Python prompt and the results didn't look good for faking this macro with eval:

def foo(x): return 2 times (x + 3)

> foo(1)

8

(not a full version of debugExpr, but enough to test:)

def debugExpr(expr): print "Expr is " + expr return eval(expr)

def bar(x): return 2 times debugExpr('x + 3')

> bar(1)

Expr is x + 3

Traceback (most recent call last):

File "", line 1, in ?

File "", line 2, in bar

File "", line 3, in debugExpr

File "", line 0, in ?

NameError: name 'x' is not defined

PS: I couldn't use the asterisk symbol in the post, so I used 'times' instead.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: