Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Inheriting and modifying lots of methods

Reply
Thread Tools

Inheriting and modifying lots of methods

 
 
Felipe Ochoa
Guest
Posts: n/a
 
      10-27-2009
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!
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-27-2009
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-27-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheriting methods but over-riding docstrings Steven D'Aprano Python 16 01-22-2010 05:13 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Looking for lots of words in lots of files brad Python 9 06-19-2008 07:59 AM
Inheriting methods - what to put in super class. Ook Java 3 02-22-2007 05:09 AM
Downloading lots and lots and lots of files coolneo Perl Misc 9 01-30-2007 02:34 PM



Advertisments