Live data from Hacker News

Macros in Python

github.com

1–10 of 71 posts

Re: Macros in Python

#4
It seems like the give-up on clarity of code isn't worth of it for most of this, but the tail call optimization is a nifty little feature to have.

Re: Macros in Python

#7
While the specific feature is fun to see in python, I'm much more impressed by the fact that MacroPy enables you to do AST transformations from python modules on the fly. This allows for developing DSLs like they show with PINQ (a LINQ "clone").

Re: Macros in Python

#8
post #2

Can 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? :)

Re: Macros in Python

#9
post #2

Can anyone give a Tl;DR about how this works? Don't x and y need to exist beforehand?

From the opening paragraph of the readme.md

> 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

#10
post #8
post #2

Can 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? :)

I'm sure there's a use-case for this, but I'm not coming up with one off the top of my head. This comes across as an extremely rare type of problem to encounter.

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)
Post reply on HN