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

Дамјан Георгиевски gdamjan at gmail.com
Sat May 9 23:04:17 EDT 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