4 ms·
i actually think both Django and Tensorflow would benefit from macros. in both cases you spend a lot of time writing what's really a DSL embedded into Python (d
by uryga 6y ago
i actually think both Django and Tensorflow would benefit from macros. in both cases you spend a lot of time writing what's really a DSL embedded into Python (defining models using a magic metaclass thingy / defining computation graphs, basically writing ASTs by hand).
---
for another example, consider
Foo = namedtuple('Foo', ['a', 'b'])
where the namedtuple function actually has to hack around the fact that it's not a macro: https://github.com/python/cpython/blob/644e94272a89196801825cb69a56377bf62d256a/Lib/collections/__init__.py#L498 https://github.com/python/cpython/blob/644e94272a89196801825...
tldr: Foo needs to have
__module__ = 'yourmodule'
__qualname__ = 'yourmodule.Foo'
or else stuff like Pickle won't work right. so namedtuple has to reach into the caller's stackframe to see what module it was called from! this wouldn't be an issue if it was a macro and just expanded to a class definition.
- Daishiman 6y agoSo, I agree that it would look slightly better if it were a macro. Having said that, is it worth the additional language complexity and opening a Pandora's box of footguns? No. The idioms are not _ideal_, but I would not consider that to be dramatically painful, not would I even say it slows down development much. I think it's very difficult to improve idioms at this point without adding complexity in the language to the point where, IMO, it's no longer worth it. I think a strong point of Python vs other languages in the same class is that there's a well-defined limit, both as a community language and language capabilities, as to how much complexity and rope you're willing to give the user.