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
I want to use macro to enable sql statement in python code. - select * from table1
I don't think Python needs macros, because they are useful for 2 things:
Creating a DSL or more eloquent syntax for something (Lisp LOOP macro is a nice example). In this case, Python philosophy decided against it deliberately. If there is some explicit notation you're missing, you can always ask for a PEP.
Making things faster by precomputing things at compile time. Python isn't oriented to speed, so you can always use a function instead.
I am not saying macros are wrong, just that they don't fit Python philosophy. You can always do without them without much code duplication, because you have duck typing and operator overloading.
And as a side note, I would much rather see Lisp's restarts in Python than macros.
Read "The Lambda Papers" so you might find out generally why one would take advtage of macros at all.
You should start with ‘AIM-353 Lambda:The Ultimate Imperative’ and follow it with ‘AIM-443 Lambda: The Ultimate GOTO’. Both may be found here:
http://library.readscheme.org/page1.html
Coming from a C-world, I'd really appreciate being able to do efficient logging of rich messages. Instead of writing
if logger.level > logger.DEBUG:
logger.log('%s' % (an_expensive_call_that_gives_useful_info(),))
with macros, one could instead do something like
DEBUG('%s' % (an_expensive_call_that_gives_useful_info(),))
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.
Hy, For my own use, I created a Python module (Espy) that allows macro definitions with arguments, loop and conditional code generation: You create a source.espy file, then launch the appropriate function, then source.py is generated.
It allows syntaxes as following:
macro repeat(arg1):
for i in range(%arg1%):
socket
print "stop"
...
repeat(5):
print "Hi everybody"
print "See you soon"
is equivalent to:
...
for i in range(5):
print "Hi everybody"
print "See you soon"
print "stop"
Other syntax:
macro doit(arg1):
for i in %arg1%:
socket suit(arg2):
socket
print %arg2%
socket check(arg3):
if %arg2%==%arg3%:
socket
...
#use
doit(range(10)):
suit(result):
result=i*i
check(result,25):
print "I knew that 5*5 == 25"
is equivalent to:
for i in range(10):
result=i*i
print result
if result==25:
print "I knew that 5*5 == 25"
More, Espy has 2 functions: "macro for" and "macro if". An example:
macro for v in [6,10,12,20,23]:
macro if 7<%v%<22:
True:
print "At %v%, I'm awake."
False:
print "At %v%, I'm sleeping."
is translated by Espy in:
print "At 6, I'm sleeping."
print "At 10, I'm awake."
print "At 12, I'm awake."
print "At 20, I'm awake."
print "At 23, I'm sleeping."
Complete documentation and free download can be found here: http://elp.chronocv.fr
I use this module in many cases. It permits more structured and shorter codes. With it I generated 65000 lines of clear and efficient python code from 1000 lines of espy code for a new chess engine project (still in progress).
If Python could include macros in futur release, it'd become more impressive.