creating classes with mix-ins

samwyse samwyse at
Mon May 11 17:44:03 EDT 2009

On May 11, 1:16 pm, samwyse <samw... at> wrote:
> I'm writing a class that derives it's functionality from mix-ins.

While waiting, I gave a try at using class decorators.  Here's what I
came up with:

def add_methods(*m_list, **kwds):
    def wrapper(klass):
        for m_name in m_list:
            def template(self, which, username, password, *args):
                if not, password,
which, m_name):
                    raise Exception('Unauthorised access')
                return getattr(, m_name)(which, *args)
            dotted_name = kwds.get('prefix', '') + m_name
            template.__name__ = dotted_name
            template.__doc__ = dotted_name
            setattr(klass, dotted_name, template)
        return klass
    return wrapper

@add_methods('newPost', 'editPost', 'getPost', 'newMediaObject',
             'getCategories', 'getRecentPosts',
class MetaWeblog(object):
    def __init__(self,
                 blogHandler=SimpleBlogHandler): = securityHandler() = blogHandler()

if __name__ == '__main__':
    server = SimpleXMLRPCServer(("localhost", 8080))

The problem that I'm having is that when I call newPost,
SimpleBlogHandler.getRecentPosts gets invoked.  Apparently add_methods
isn't generating unique templates.  I think I need to move 'template'
into another function, but I'm starting to wonder if metaclasses might
work better.  Any ideas?

More information about the Python-list mailing list