Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass an undefined method call to an attribute containing a different object

I have a class with an attribute that will be a pandas DataFrame(). I'd like to set up the class such that if any DataFrame() method is called on it, that method will be applied to the DataFrame().

I have set up this scenario already:

import pandas as pd
class A(object):
    def __init__(self):
        self.df = pd.DataFrame()
        self.name = 'Charlie'

    def get_name(self):
        return self.name

    def head(n=5):
        return self.df.head(n)

So I can do this:

a = A()
a.get_name()

Out[1]: 'Charlie'

a.head()

Out[2]: Empty DataFrame()

I don't want to redefine each and every method of a class to pass through. This is just one example. I'd like to apply this elsewhere.

Thanks,

Pi

like image 497
piRSquared Avatar asked Feb 06 '14 17:02

piRSquared


Video Answer


1 Answers

You need to override the __getattr__ method of class A and delegate all calls to self.df and in case the attribute is not found in self.df then look for it in the current object. I don't have Pandas on my system, so I'll define another class for the demo:

class B(object):

    def func(self):
        print 'Inside B'

class A(object):

    def __init__(self):
        self.df = B()
        self.name = 'Charlie'

    def get_name(self):
        return self.name

    def __getattr__(self, attr):
        try:
            return getattr(self.df, attr)
        except AttributeError:
            #If attribute was not found in self.df, then try in self
            return object.__getattr__(self, attr)

Output:

>>> a = A()
>>> a.func()
Inside B
>>> a.get_name()
'Charlie'
like image 142
Ashwini Chaudhary Avatar answered Oct 10 '22 11:10

Ashwini Chaudhary