No macros in Python

Beni Cherniavsky cben at techunix.technion.ac.il
Sun Dec 15 19:52:23 EST 2002


On 2002-12-15, Mel Wilson wrote:

>  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.
>
# Untested
for label, field in [('Key No.', 'person_id'),
                     ('First Name', 'fname'),
                     ('Last Name', 'lname')]:
    hs = wxBoxSizer (wxHORIZONTAL)
    hs.Add (wxStaticText (self, -1, label, wxDefaultPosition,
                          labelsize))
    ctrl = wxTextCtrl (self, -1, '', wxDefaultPosition)
    hs.Add (self, ctrl, 1)
    vs.Add (hs, 0, wxGROW)
    self.__dict__[field + '_ctrl'] = ctrl

Variable assignment is not the real point of macros IMHO.  Also, if you
only need a given macro in one place in the program, it's not a good
candidate.  It's too late in the night to think of really good examples
for what is :-).

-- 
Beni Cherniavsky <cben at tx.technion.ac.il>




More information about the Python-list mailing list