In [12]:
def foo(x, y):
    return x.bar(y) * 2
In [13]:
class Test1:
    def bar(self, arg):
        return len(arg)
    
class Test2:
    def bar(self, arg):
        return arg.split(" ")
    
    
In [14]:
t1 = Test1()
t2 = Test2()

foo(t1, [1,2,3,4])
Out[14]:
8
In [15]:
foo(t2, "is this the real life? Or is this just fantasy")
Out[15]:
['is',
 'this',
 'the',
 'real',
 'life?',
 'Or',
 'is',
 'this',
 'just',
 'fantasy',
 'is',
 'this',
 'the',
 'real',
 'life?',
 'Or',
 'is',
 'this',
 'just',
 'fantasy']

More Useful Example

In [16]:
[1,2,3,4] + 5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-39bd20c841d7> in <module>()
----> 1 [1,2,3,4] + 5

TypeError: can only concatenate list (not "int") to list
In [17]:
class MyList(list):
    
    def __add__(self, x):
        self.append(x)
In [18]:
ml = MyList([1,2,3,4])
In [19]:
ml 
Out[19]:
[1, 2, 3, 4]
In [20]:
ml + 5
In [21]:
ml
Out[21]:
[1, 2, 3, 4, 5]
In [ ]:
 
In [ ]: