Results 1 to 4 of 4

Thread: Best Python Style - providing a wrapper class

  1. #1
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Best Python Style - providing a wrapper class

    Is there a generic python way of creating a simple super class that will extend every method in the base class, without needing to explicitly superseed every base class method.

    In nearly all cases the extension will be exactly the same functionality added around the call to the base class method - and the exceptions are trivial.

    What i think i need is to define a method on my super class, which is a catch all (i.e. is called when any method is invoked) and then implement my own functionality around the call to the specific Base class method.

    clearly this catch all method would need to be able to handle variable arguments, and i would need to be able to use a string (i assume) to call a Base class method.

    Is all of this possible ?
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  2. #2
    Join Date
    Nov 2006
    Location
    Israel
    Beans
    765
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Best Python Style - providing a wrapper class

    Sure this is possible - through the use of the getattr slot (__getattr__):
    Code:
    class Wrapper(object):
    	def __init__(self, obj):
    		self.obj = obj
    		
    	def __getattr__(self, name):
    		func = getattr(self.__dict__['obj'], name)
    		if callable(func):
    			def my_wrapper(*args, **kwargs):
    				print "entering"
    				ret = func(*args, **kwargs)
    				print "exiting"
    				return ret
    			return my_wrapper
    		else:
    			return func
    As you can see, we return our own function whenever the user calls one of the original functions. (this is very similar to decorators)
    To use:
    Code:
    ## for example on a string:
    s = 'abc'
    w = Wrapper(s)
    print w.isdigit()
    If you want to change what happens on every call, simply change the my_wrapper function.

    EDIT:
    Note also the *args, **kwargs that my_wrapper receives as arguments. This is how you do variable argument passing in python. args is a tuple that catches all of the normal, non-keyword arguments, and kwargs is a dictionary between each keyword and its value.
    Last edited by smartbei; August 22nd, 2009 at 11:52 AM.
    Intel E6300 / MSI P4M890M / 2GB DDR2 677 / 80GB + 1TB SATA2 / GeForce 6200TC / DL DVD+-RW / Dell 24" U2410

  3. #3
    Join Date
    May 2006
    Location
    Victoria, Australia
    Beans
    648
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Best Python Style - providing a wrapper class

    Another way is to design your classes such that you extend only a part of the function.

    So for example, we have an object where we must called object.execute() which involves printing some generic message, some processing, followed by another generic message.

    And depending on the current state of the program, different processing is required.

    we could do something like

    PHP Code:
    class defaultExecutor(object):
        
    def execute(self, *args, **kwargs):
            print 
    "hello there"
            
    self.process(*args, **kwargs)
            print 
    "done stuff"

        
    def process(self, *args, **kwargs):
            
    """this becomes the method to be overridden"""
            
    1
            x 
    += 1
            
    print x

    class OtherExecutor(defaultExecutor):
        
    def process(self, *args, **kwargs):
            return 

    To make life nice you'd design it so that process has some specific set of arguements instead of using * and ** magic (unless of course that would be appropiate)

    I tend to do this when I have classes that all need to do the same thing in __init__ but still might require something different in each case, so I don't have to overwrite __init__ and do super calls and such.


    Also, another way is to use function decorators.

    for example

    PHP Code:
    def decorator(func):
        
    def wrapped(*args, **kwargs):
            print 
    "hello there"
            
    func(*args, **kwargs)
            print 
    "done stuff"
        
    return wrapped

    class otherExecutor(object):
        @
    decorator
        def execute
    (self, *args, **kwargs):
            return 

    if u find some of my ideas weird...look at my avatar for the reason
    http://delfick.storage.googlepages.c...ycfuserbar.png

  4. #4
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Best Python Style - providing a wrapper class

    thank you both ...

    Most helpful.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •