Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Ruby's method_missing in Python...
import unittest
from functools import partial
class MethodMissing:
def method_missing(self, name, *args, **kwargs):
'''please implement'''
raise NotImplementedError('please implement a "method_missing" method')
def __getattr__(self, name):
return partial(self.method_missing, name)
class Wrapper(object, MethodMissing):
def __init__(self, item):
self.item = item
def method_missing(self, name, *args, **kwargs):
if name in dir(self.item):
method = getattr(self.item, name)
if callable(method):
return method(*args, **kwargs)
raise AttributeError(' %s has not method named "%s" ' % (self.item, name))
class Item(object):
def __init__(self, name): = name
def test(self, string):
return string + ' was passed on'
class EmptyWrapper(object, MethodMissing):
'''not implementing a missing_method'''
class TestWrapper(unittest.TestCase):
def setUp(self):
self.item = Item('test')
self.wrapper = Wrapper(self.item)
self.empty_wrapper = EmptyWrapper()
def test_proxy_method_call(self):
string = self.wrapper.test('message')
self.assertEqual(string, 'message was passed on')
def test_normal_attribute_not_proxied(self, ):
with self.assertRaises(AttributeError):
def test_empty_wrapper_raises_error(self, ):
with self.assertRaises(NotImplementedError):
if __name__ == '__main__':

gterzian commented Aug 31, 2013

The problem with this implementation is that Python expects getattr to return a callable if a 'missing method' is called on an object. Python will then proceed to call this returned callable and the client code will receive the value returned by this method call.
So the only thing you can do is grab the method from the proxied object and hand it over to Python.
In Ruby method_missing provides much greater flexibility: you can do whatever you want within you implementation of method_missing, and the return value of this method will be returned directly to the client code.
In this Python implementation you can only forward the call and hope the return value of the method call is what the client code expects.


gterzian commented Aug 31, 2013

Update: solve the above issue, latest implementation does provide very similar(identical?) functionality as Ruby's method_missing concept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment