[Python-Dev] A Hygienic Macro System in Python? (original) (raw)
Daniel Mahler mahler@cyc.com
Mon, 18 Mar 2002 19:54:28 -0600
- Previous message: [Python-Dev] A Hygienic Macro System in Python?
- Next message: [Python-Dev] A Hygienic Macro System in Python?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I this Skip Montanaro writes:
Tim> OTOH, there are cases like Tim> lock.acquire() Tim> try: Tim> do stuff Tim> finally: Tim> lock.release()
===>
Tim> withlock lock: Tim> do stuff ...
Using the same argument as "if Guido wanted 'unless' he'd add it to the language", I think that if this is important enough, it's important enough to add to the language. I think a "lock" keyword would be appropriate:
lock somelock: do stuff
I think this misses a more general point behind Tims post. There are many APIs that lead you to write code with certain fixed parts. Since Python like most languges has eager evaluation, procedures cannot be used to abstract out the fixed parts (usually initialization and clean up, but not always)
eg:
#imagine this is some connection protocol for some kind of server
withConnectionTo connVar address:
==>
connVar = Connect(address) try: finally: connVar.disconnect()
[ I presume nobody really wants people to implement
def withConnectionTo(connVar,address,statements): exec("""
%s = Connect(%s) try: %s finally: %s.disconnect()
""" % (connVar,address,statements, connVar)
)
]
There is a potential infinity of code templates like this that can get annoying, and which cannot be abstracted out procedurally in a natural way.
I think the thing worth noting here is that the introduced constructs would usually be colon&indent type entities (what do we call those?) One application of definable syntax is compositional incremental output without the need to build up a string or some other kind of datastructure to represent the entire document first which can get horribly expensive for complex documents. Consider a html printer macro library:
html: header: title: print "blah" body: h1: print "bleh" p: print "foo" a (href="http://bar.org"): print "bar" print "baz"
Daniel Mahler
- Previous message: [Python-Dev] A Hygienic Macro System in Python?
- Next message: [Python-Dev] A Hygienic Macro System in Python?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]