On Jul 13, 2019, at 12:16, Nima Hamidi
In the following, I sketch how I think this can be implemented: Let BoundExpression be a class containing an ast.Expression as well as locals and globals dictionaries. BoundExpression can also have an eval method that evaluates its expression using its locals and globals dictionaries. Let `x` be the short form for BoundExpression("x", locals(), globals()). Then, the plot function can be implemented in a way that plot(`x`, `y`) draws scatter plot of x and y and also, labels the axes correctly. No need to provide labels explicitly anymore. A more challenging idea is to let developers decide whether their functions need NSE or not. For example, when a function is defined as def f(`x`), for any method call like f(x), the first argument should be wrapped with a BoundExpression instance.
Would it be easy to build something like this on top of a more general macro system, like MacroPy? If so, you could create a proof of concept using existing Python and get useful feedback. As it is, with just an explanation and a single example, it’s hard to really evaluate the idea. Also, what happens if the expression contains, say, a := assignment? In that case, calling eval against locals() doesn’t have the same effect as normal evaluation. Is that a problem, or is that behavior that people would and should expect when they ask for a bound expression instead of a value? In your example, it seems like you could evaluate it normally and pass in the value along with the AST, avoiding that problem, and making everything a lot simpler. I can imagine uses where that wouldn’t be sufficient, but they all seem like the kind of thing that demands a full macro system, so I’m not sure they’re relevant. Do you have examples where you need to eval on demand? For that matter, it seems like your example could be handled by just defining `x` to mean, say, ('x', x), without needing a live AST at all. What are the examples where the string form of the expression isn’t sufficient but this proposal is?