public
Last active

Ruby's method_missing in Python...

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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)
else:
raise AttributeError(' %s has not method named "%s" ' % (self.item, name))
 
class Item(object):
def __init__(self, name):
self.name = name
def test(self, string):
return string + ' was passed on'
class EmptyWrapper(object, MethodMissing):
'''not implementing a missing_method'''
pass
 
 
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):
self.wrapper.name
self.wrapper.name()
def test_empty_wrapper_raises_error(self, ):
with self.assertRaises(NotImplementedError):
self.empty_wrapper.test('message')
 
if __name__ == '__main__':
unittest.main()

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.

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.