Python宏:用例? [英] Python Macros: Use Cases?

查看:65
本文介绍了Python宏:用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果Python具有类似于Lisp/Scheme的宏功能(例如 MetaPython ) ,您将如何使用它?

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

如果您是Lisp/Scheme程序员,那么您将使用什么类型的宏(除了在Python中具有清晰的句法并行性(例如while循环)的宏之外)?

If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel in Python such as a while loop)?

推荐答案

一些Lisp宏示例:

  • ITERATE ,它是一种有趣且可扩展的循环工具
  • CL-YACC / CL-WHO ,它可以指定带有静态和动态部分的html文档
  • Parenscript 这是一个JavaScript代码生成器
  • 各种简单的代码包装器,例如错误处理程序(我有一个with-gtk-error-message-handler,它执行代码并在发生未处理的错误时显示GtkMessageDialog),执行程序(例如,给定代码,以不同的方式执行它线程;我有一个在主线程内的宏,可以在不同线程中执行代码; PCall 库使用宏包装要同时执行的代码)
  • 具有宏的GUI构建器(例如,指定窗口小部件层次结构和窗口小部件的属性,并使用宏生成用于创建所有窗口小部件的代码)
  • 在编译期间使用外部资源的代码生成器.例如,处理C标头并生成FFI代码的宏,或根据数据库模式生成类定义的宏
  • 声明性FFI.例如,指定外部结构,函数,它们的参数类型并具有宏以生成相应的lisp结构,具有类型映射和封送处理代码的函数
  • Common Lisp的基于连续性的Web框架使用宏将代码转换为CPS(连续传递样式)形式.
  • ITERATE which is a funny and extensible loop facility
  • CL-YACC/FUCC that are parser generators that generate parsers at compile time
  • CL-WHO which allows specifying html documents with static and dynamic parts
  • Parenscript which is a javascript code generator
  • Various simple code-wrappers, e.g., error handlers (I have a with-gtk-error-message-handler that executes code and shows GtkMessageDialog if unhandled error occurs), executors (e.g., given a code, execute it in different thread; I have a within-main-thread macro that executes code in different threads; PCall library uses macros to wrap code to be executed concurrently)
  • GUI builders with macros (e.g., specify widgets hierarchy and widgets' properties and have a macro generate code for creation of all widgets)
  • Code generators that use external resources during compilation time. E.g., a macro that processes C headers and generates FFI code or a macro that generates classes definitions based on database schema
  • Declarative FFI. E.g., specifying the foreign structures, functions, their argument types and having macros to generate corresponding lisp structures, functions with type mapping and marshaling code
  • Continuations-based web frameworks for Common Lisp use macros that transform the code into CPS (continuation passing style) form.

这篇关于Python宏:用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆