No macros in Python
Mel Wilson
mwilson at the-wire.com
Sun Dec 15 18:56:31 EST 2002
In article <3DFCDAB5.5FBA7730 at engcorp.com>,
Peter Hansen <peter at engcorp.com> wrote:
>Mike Meyer wrote:
>>
>> Python doesn't have macros. On the other hand, looking at the world
>> through OO glasses rather than S-expression glasses means you don't
>> need macros as often. But the topic comes up at irregular intervals,
>> so someone may eventually come up with a way of doing macros in Python
>> that is right - at which point it'll be added.
>
>I haven't heard this expressed as a disadvantage very often, perhaps
>not before in this group since I've been reading it.
>In what way would macros (as I understand them... from C for example)
>improve Python, other than possibly a small improvement in performance?
If you must have such a thing as C macros, it's
available. I tried this as a way of simplifying the
creation of a lot of edit boxes with wxPython:
new_edit_box_macro = """\
hs = wxBoxSizer (wxHORIZONTAL)
hs.Add (wxStaticText (self, -1, '%(label)s', wxDefaultPosition, labelsize)
self.%(field)s_ctrl = wxTextCtrl (self, -1, '', wxDefaultPosition)
hs.Add (self, %(field)s_ctrl, 1)
vs.Add (hs, 0, wxGROW)"""
exec (new_edit_box_macro % {'label':'Key No.', 'field':'person_id'})
exec (new_edit_box_macro % {'label':'First Name', 'field':'fname'})
exec (new_edit_box_macro % {'label':'Last Name', 'field':'lname'})
and so on.
A function would look cleaner, except that local values
like `vs` and `labelsize` would have to be explicitely
passed in (in the older Python versions that I use.) I
gather that exec runs its code in the local name space.
Regards. Mel.
More information about the Python-list
mailing list