How can I dynamically insert a base class in a given class

Дамјан Георгиевски gdamjan at gmail.com
Sun May 10 05:04:17 CEST 2009


How can I dynamically insert a base class in a given class? Yeah, I'm 
writing a class decorator that needs to manipulate the class by 
inserting another base class in it.


Something like:

class ReallyBase(object):
   def someimportantmethod(self):
     return 'really really'

@expose(args)
class Unsuspecting(object):
   def stuff(self):
     return "ok"

I've come up with this ... but I'm sure it has some shortcomings 

def expose(args):
    def wrap(cls):
       # do stuff with args and cls
       return type(cls.__name__, cls.__bases__ + (ReallyBase,), 
dict(cls.__dict__))
    return wrap



-- 
дамјан ( http://softver.org.mk/damjan/ )

war is peace
freedom is slavery
restrictions are enablement




More information about the Python-list mailing list