Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Macros: Use Cases?

Tags:

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 you use macros for (other than things that have a clear syntactic parallel in Python such as a while loop)?

like image 284
Rick Copeland Avatar asked Apr 18 '09 22:04

Rick Copeland


People also ask

What is macro case in Python?

A macro is a compile-time function that transforms a part of the program to allow functionality that cannot be expressed cleanly in normal library code. The term “syntactic” means that this sort of macro operates on the program's syntax tree.

Can Python be used for macros?

You can write an Excel macro in python to do whatever you would previously have used VBA for. Macros work in a very similar way to worksheet functions. To register a function as a macro you use the xl_macro decorator. Macros are useful as they can be called when GUI elements (buttons, checkboxes etc.)

What is a macro in programming?

What Does Macro Mean? A macro is an automated input sequence that imitates keystrokes or mouse actions. A macro is typically used to replace a repetitive series of keyboard and mouse actions and used often in spreadsheets and word processing applications like MS Excel and MS Word.

How do you create a macro variable in Python?

Call PROC PYTHON and assign the value of Myvar to a Python variable. Assign the value of the SAS macro variable Myvar to the Python variable x by using the SAS. symget callback method. Use the Python print function to display the value of x .


2 Answers

I believe that macros run counter to Python's culture. Macros in Lisp allow the big ball of mud approach; you get to redefine the language to become more suited to your problem domain. Conversely Pythonic code uses the most natural built in feature of Python to solve a problem, instead of solving it in a way that would be more natural in a different language.

Macros are inherently unpythonic.

like image 77
RossFabricant Avatar answered Oct 20 '22 05:10

RossFabricant


This is a somewhat late answer, but MacroPy is a new project of mine to bring macros to Python. We have a pretty substantial list of demos, all of which are use cases which require macros to implement, for example providing an extremely concise way of declaring classes:

@case class Point(x, y)  p = Point(1, 2) print p.x   # 1 print p     # Point(1, 2) 

MacroPy has been used to implement features such as:

  • Case Classes, easy Algebraic Data Types from Scala
  • Pattern Matching from the Functional Programming world
  • Tail-call Optimization
  • Quasiquotes, a quick way to manipulate fragments of a program
  • String Interpolation, a common feature in many languages, and Pyxl.
  • Tracing and Smart Asserts
  • PINQ to SQLAlchemy, a clone of LINQ to SQL from C#
  • Quick Lambdas from Scala and Groovy,
  • Parser Combinators, inspired by Scala's.

Check out the linked page to find out more; I think I can confidently say that the use cases we demonstrate far surpass anything anyone's suggested so far on this thread =D

like image 34
Li Haoyi Avatar answered Oct 20 '22 05:10

Li Haoyi