[Python-ideas] History on proposals for Macros?
Steven D'Aprano
steve at pearwood.info
Sat Mar 28 18:26:24 CET 2015
On Sat, Mar 28, 2015 at 09:53:48AM -0700, Matthew Rocklin wrote:
[...]
> The goal is to create things that look like functions but have access to
> the expression that was passed in.
> assertRaises(ZeroDivisionError, 1/0) # Evaluate the rhs 1/0 within
> assertRaises function, not before
> Generally one constructs something that looks like a function but, rather
> than receiving a pre-evaluated input, receives a syntax tree along with the
> associated context. This allows that function-like-thing to manipulate the
> expression and to control the context in which the evaluation occurs.
How will the Python compiler determine that assertRaises should receive
the syntax tree rather than the evaluated 1/0 expression (which of
course will raise)? The information that assertRaises is a "macro" is
not available at compile time.
I really like the idea of delaying the evaluation of certain expressions
until a time of the caller's choosing, but I don't see how to make that
work automatically. Of course we can do it manually by wrapping the
expression in a function, or by writing it as a string and compiling it
with compile() for later eval'ing, but the sort of thing you show above
strikes me as fundamentally incompatible with Python's execution model.
Happy to be proven wrong though.
--
Steve
More information about the Python-ideas
mailing list