Hacker News new | past | comments | ask | show | jobs | submit login

If macros are used responsibly, they can make a program more readable.

I have to write lots of boilerplate code for certain things that in other languages would be much simpler; for example when I'm doing pattern matching in Python I have to use a sequence of if statements and complex expressions. I'd love a pattern matching macro because that would let me express my logic in a manner which more closely matches the semantics.

Macros can be abused just like decorators, classes, or functions to make complex code that's difficult to understand or modify. While macros give you a bit more rope, used properly they can be very handy.




> If macros are used responsibly, they can make a program more readable.

(AST based) Macros can really be an asset in a statically typed language (like Rust or Crystal), they are crap in C since it's text substitution [1], but an interpreted language that already has a lot of meta programming capabilities doesn't really need macros. Python has problems, but you don't fix those with Macros on top of them. They will never be used responsibly.

1: With a better macro system C could have been a much better and secure language IMHO.


Metaprogramming has a runtime cost that macros (even crappy C-like ones) don't. In the case of an interpreted language, this is an important feature.

Having said that, I haven't seen metaprogramming being overused or abused much in Python.


You shouldn't be using Python in the first place if you're worried about the runtime cost of metaprogramming.


If macros offer the same benefit at no additional cost, why not?


Macros have decidedly nonzero cost. Look at the discussion from the core devs surrounding this; just the implementation is a major change for the parsing and AST modules with unclear performance implications. Introducing this much complexity to the language is far from "no cost" as well. I predict that if this PEP goes through, it will be a watershed moment for Python and the language will become an unrecognizable mess (more so than already) within a few years. If GvR was still in charge, I would be sure that this would be dismissed. Now, I am not so sure, and that troubles me.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: