c macros in python.

John Machin sjmachin at lexicon.net
Sun May 6 17:09:49 EDT 2007


On May 7, 7:01 am, noagbodjivic... at gmail.com wrote:
> Hey,
>
> I'm writing a script to generate code. I'm a bit tired of typing
> outfile.write(....). Does python have a way to c-like macros? Every
> instance of o(...) in the code will be replaced by outfile.write(...)?

Functions and methods are first-class citizens in Python; all you have
to do is:

o = outfile.write
o('blah blah\n')
o('etc\n')

Bonus: your code will run (measurably but almost imperceptibly)
faster, because you save a method lookup every time you use it.This
would not happen with a text-substituting macro approach.

HTH,
John




More information about the Python-list mailing list