Inheriting and modifying lots of methods

Discussion in 'Python' started by Felipe Ochoa, Oct 27, 2009.

  1. Felipe Ochoa

    Felipe Ochoa Guest

    I need to create a subclass from a parent class that has lots of
    methods, and need to decorate all of these. Is there pythonic way of
    doing this other than:

    def myDecorator (meth):
    @wraps(meth)
    def newMeth (*args, **kw):
    print("Changing some arguments here.")
    return meth(newargs, **kw)
    return newMeth

    class Parent:
    def method1(self,args):
    pass
    def method2(self,args):
    # and so on...
    def methodn(self,args):
    pass

    class Mine(Parent):
    for thing in dir(Parent):
    if type(eval("Parent."+thing)) == type(Parent.method1):
    eval(thing) = myDecorator(eval("Parent." + thing))

    I'm not even sure that this works! Thanks a lot!
     
    Felipe Ochoa, Oct 27, 2009
    #1
    1. Advertisements

  2. Felipe Ochoa wrote:

    > I need to create a subclass from a parent class that has lots of
    > methods, and need to decorate all of these. Is there pythonic way of
    > doing this other than:
    >
    > def myDecorator (meth):
    > @wraps(meth)
    > def newMeth (*args, **kw):
    > print("Changing some arguments here.")
    > return meth(newargs, **kw)
    > return newMeth
    >
    > class Parent:
    > def method1(self,args):
    > pass
    > def method2(self,args):
    > # and so on...
    > def methodn(self,args):
    > pass
    >
    > class Mine(Parent):
    > for thing in dir(Parent):
    > if type(eval("Parent."+thing)) == type(Parent.method1):
    > eval(thing) = myDecorator(eval("Parent." + thing))
    >
    > I'm not even sure that this works! Thanks a lot!


    It's not working, you can't assign to something eval'ed.

    There are various options here, including metaclasses, but I think the
    simplest is to write a simple function:

    def decorate_methods(subclass):
    for name in dir(subclass):
    thing = getattr(subclass, name)
    if isinstance(thing, types.MethodType):
    setattr(subclass, name, myDecorator(thing)) # maybe
    myDecorator(thing.im_func) is better here


    Simply invoke that function with "Mine" - or use a metaclass to do that
    implicitly.

    Diez
     
    Diez B. Roggisch, Oct 27, 2009
    #2
    1. Advertisements

  3. On Tue, 27 Oct 2009 10:43:42 -0700, Felipe Ochoa wrote:

    > I need to create a subclass from a parent class that has lots of
    > methods, and need to decorate all of these. Is there pythonic way of
    > doing this other than:
    >
    > def myDecorator (meth):
    > @wraps(meth)
    > def newMeth (*args, **kw):
    > print("Changing some arguments here.") return meth(newargs,
    > **kw)
    > return newMeth
    >
    > class Parent:
    > def method1(self,args):
    > pass
    > def method2(self,args):
    > # and so on...
    > def methodn(self,args):
    > pass
    >
    > class Mine(Parent):
    > for thing in dir(Parent):
    > if type(eval("Parent."+thing)) == type(Parent.method1):
    > eval(thing) = myDecorator(eval("Parent." + thing))
    >
    > I'm not even sure that this works! Thanks a lot!



    eval() won't work there. exec might, but any time you think you need eval/
    exec, you probably don't :)

    You can probably do some magic with metaclasses, but the simplest
    solution would be something like this:

    class Mine(Parent):
    pass


    import types
    for name in dir(Mine):
    attr = getattr(Mine, name)
    if type(attr) in (types.MethodType, types.UnboundMethodType):
    setattr(Mine, name, myDecorator(attr))


    The above (probably) won't touch classmethods and staticmethods.



    --
    Steven
     
    Steven D'Aprano, Oct 27, 2009
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ook
    Replies:
    3
    Views:
    519
  2. brad
    Replies:
    9
    Views:
    457
    Bruno Desthuilliers
    Jun 19, 2008
  3. Dido Sevilla

    Methods with lots of arguments

    Dido Sevilla, May 27, 2005, in forum: Ruby
    Replies:
    2
    Views:
    138
    Carlos
    May 27, 2005
  4. Kenneth McDonald
    Replies:
    5
    Views:
    488
    Kenneth McDonald
    Sep 26, 2008
  5. coolneo
    Replies:
    9
    Views:
    302
    coolneo
    Jan 30, 2007
Loading...

Share This Page