Live data from Hacker News

Macros in Python

github.com

41–50 of 71 posts

Re: Macros in Python

#41

What made you target Python 2 instead of Python 3?

Laziness; we already had it installed.

That's what a lot of other people are using.

It's not that we don't like Python3; their ASTs are considerably simpler (e.g. no print statement), they have importlib, and a bunch of other nice things. It's just that the world we interact with mostly uses Python2, so we just follow along.

Re: Macros in Python

#42
post #28

It's a cool idea, but this basically sneaks new features into the Python language through a backdoor, bypassing the public review/feedback process. I would suggest the author create a PEP for these features instead.

Macros aren't solely used to implement high-level features, mind you - in CL they're introduced without much hassle to work out gruntwork as well precisely because an elided form in s-expressions seems just as natural as the original forms.

Although that level of fragmentation you do talk about is plausible - I'd find it simultaneously disheartening and worth a bit of a chuckle. (I'm reminded of all of the concurrency abstractions CL seems "capable of" hosting with minimal friction - but none with definite thrust.)

Re: Macros in Python

#45
post #17

Something about classes that are nested inside other classes suddenly being available as top level classes seems wrong. I'm not super deep on the innards of Python, but that doesn't look right.

It definitely looks funny from a python point of view; it's inspired by ADTs and OCaml. Perhaps it would be better to leave them to be accessed via a qualified name (e.g. `List.Nil`, `List.Cons`). This is all up for debate.

A qualified name makes more sense to me. Having them exposed as top-level classes just doesn't feel right, given how they are defined.

Re: Macros in Python

#46

Wow, I am blown away by how complete this implementation is. Nice work! Now please post a writeup of how the internals work :)

It's actually pretty simple and almost all in this file:

https://github.com/lihaoyi/macropy/blob/master/macropy/core/...

Those already familiar with the imp mechanism and AST module could probably infer it from the description.

An absolutely beautiful facet of Python's nature is that with relatively little background, it's quite straightforward to know how all sorts of 'magic's and hacks work at first glance. :)

Re: Macros in Python

#47
post #28

It's a cool idea, but this basically sneaks new features into the Python language through a backdoor, bypassing the public review/feedback process. I would suggest the author create a PEP for these features instead.

That's rather the point of macros - giving programmers the ability to new features to a language.

Re: Macros in Python

#48
post #34

Earlier quoted context omitted.

The implementation of namedtuple is pretty crazy, but the interface is simple and doesn't change the language semantics.

I'd argue that "fields as a string with spaces inside, or maybe a list of strings" is changing language semantics quite a bit. We're used to namedtuples doing it like this now, but if namedtuples didn't exist, "fields as a list of strings with spaces inside, or maybe a list of strings" would definitely not make it past code review and probably get me yelled at by my future colleagues.

[deleted]

Re: Macros in Python

#49
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)

Decorators can do this? I did not know this. I thought decorators just get handed the object after initialization, so something like:

    @blah
    class Lol:
      pass
is equivalent to:

    class Lol:
      pass
    blah(Lol)
What am I missing?

Re: Macros in Python

#50
I see the docs mention that these only work when importing modules. The dev version of IPython lets you transform the AST of code that is typed in the REPL. It would be really cool to see an IPython AST transformer based on this. Here is an example from the test files in IPython: https://github.com/ipython/ipython/blob/master/IPython/core/...
Post reply on HN