Last active
May 15, 2019 16:57
-
-
Save gterzian/6400170 to your computer and use it in GitHub Desktop.
Ruby's method_missing in Python...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
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.