Macros in Python
github.com
Macros in Python
1–10 of 71 posts
Re: Macros in Python
#2Re: Macros in Python
#3Re: Macros in Python
#4Re: Macros in Python
#5Re: Macros in Python
#6[deleted]
Re: Macros in Python
#7Re: Macros in Python
#8Can anyone give a Tl;DR about how this works? Don't x and y need to exist beforehand?
The following code:
with patterns:
Foo(x, Bar(3, z))
The constructor after the Does this help? :)Re: Macros in Python
#9Can anyone give a Tl;DR about how this works? Don't x and y need to exist beforehand?
> MacroPy provides a mechanism for user-defined functions (macros) to perform transformations on the abstract syntax tree(AST) of Python code at module import time
Basically it rewrites the code before it's compiled, so no, x and y don't need to exist (compilation hasn't finished when the macro runs)
Re: Macros in Python
#10Can anyone give a Tl;DR about how this works? Don't x and y need to exist beforehand?
It's hard to give a TL;DR on the whole library, but case classes can be used with for example pattern matching ( https://github.com/lihaoyi/macropy#pattern-matching ). The following code: with patterns: Foo(x, Bar(3, z)) The constructor after the Does this help? :)
What's the purpose of only binding x and z if the first argument of Bar matches? What if it doesn't match? Is an exception thrown? Are x,z just None?
[Note: I'm talking about the example, not the @case decorator, which does seem useful]
Edit:
After reading the library examples, the explanation above is not entirely clear:
with patterns:
Foo(x, Bar(3, z))
here's the rest of the example: print x # 4
print z # 8
I was thinking that somehow a new Foo instance was created only when the pattern matched. (Oh, and an exception is thrown when the match fails)