Python Macros: Use Cases?

后端 未结 17 2083
甜味超标
甜味超标 2021-01-30 18:37

If Python had a macro facility similar to Lisp/Scheme (something like MetaPython), how would you use it?

If you are a Lisp/Scheme programmer, what sorts of things do y

17条回答
  •  隐瞒了意图╮
    2021-01-30 19:04

    Here's one real-world example I came across that would be trivial with macros or real metaprogramming support, but has to be done with CPython bytecode manipulation due to absence of both in Python:

    http://www.aminus.net/dejavu/chrome/common/doc/2.0a/html/intro.html#cpython

    This is how the problem is solved in Common Lisp using a combination of regular macros, and read-macros to extend the syntax (it could have been done without the latter, but not the former):

    http://clsql.b9.com/manual/csql-find.html

    The same problem solved in Smalltalk using closures and metaprogramming (Smalltalk is one of the few single-dispatch OO languages that actually gets message passing right):

    http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg02096.html

    Here I tried to implement the Smalltalk approach in Common Lisp, which is a good illustration of how metaprogramming is poorly supported in the latter:

    http://carcaddar.blogspot.com/2009/04/closure-oriented-metaprogramming-via.html

提交回复
热议问题