Live data from Hacker News

Macros in Python

github.com

11–20 of 71 posts

Re: Macros in Python

#12
post #10
post #8

Earlier quoted context omitted.

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 t…

Author here. The `The purposes of pattern matching is to replace code that looks like this:

    if  (isinstance(tree, BinOp)
            and type(tree.left) is Name
            and type(tree.op) is Mod
            and tree.left.id in module.expr_registry):
        ...
with code that looks like this

    if BinOp(Name(id), Mod(), body) 
Which looks much nicer, and more clearly says what you want: that `tree` "looks like" a particular shape.

EDIT: Here's another example. Turning this:

    if  ((isinstance(tree, ClassDef) or isinstance(tree, FunctionDef))
            and len(tree.decorator_list) == 1
            and tree.decorator_list[0]
            and type(tree.decorator_list[0]) is Name
            and tree.decorator_list[0].id in module.decorator_registry):
        ...
into:

    if  ((isinstance(tree, ClassDef) or isinstance(tree, FunctionDef))
            and [Name(id)] 
Doesn't quite work yet, but we're getting there

Re: Macros in Python

#13
post #10
post #8

Earlier quoted context omitted.

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 t…

With pattern matching, the term on the left is a destructuring of the term on the right. Using a static instance like that isn't what you normally do in practice, but rather something like:

    myfoo = Foo(4, Bar(3, 8))
    with patterns:
        Foo(x, Bar(3, z)) 
So in this case, the assertion will only pass if the Foo contains a Bar in the second slot, and that Bar contains a 3 in its first slot, while also binding the 4 and 8 to their own names, x and z respectively.

Re: Macros in Python

#14
post #3

[deleted]

ADTs are a more general feature and one that I've missed in Python on more than one occasion.

I'm a little sad that the enum PEP got accepted, without them being actual ADTs.

Re: Macros in Python

#15
If you're interested in doing some of this stuff in JS, there's sweet.js[1] for macros. I've also implemented some similar stuff as libraries: adt.js[2], matches.js[3], and tailrec.js[4]. By implementing them as libraries, I've given up some of the nicety of native-looking syntax, but it requires no preprocessing and only ES3.

[1] http://sweetjs.org/

[2] https://github.com/natefaubion/adt.js

[3] https://github.com/natefaubion/matches.js

[4] https://github.com/natefaubion/tailrec.js

Re: Macros in Python

#16
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.

Re: Macros in Python

#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.

Re: Macros in Python

#18
This is really impressive. The whole contents of that module are fantastic. Ever since I've started to play around with Scala, I've missed case classes and pattern matching in Python. The ability to write AST macros is even cooler.

Re: Macros in Python

#19
post #11
post #3

[deleted]

Author here. If you look at my github, you'll see I do use plenty of Scala!

Well yes; the code is clearly the work of someone who uses and likes scala. My point was: the library seems designed to let you write "scala in python"; wouldn't it be better to use scala when you want scala syntax, and write more idiomatic python when using python?

Re: Macros in Python

#20
This is awesome. Everything here (except the anonymous arguments to lambdas, seriously, just use x y and z as default argument names) should be in Python 4 or whatever you'd want to call the successor to Python.

I love love love the PEG implementation.

Post reply on HN