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)?
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.
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 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.
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 .
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With