0

I wanted to do some handling of undefined methods or attributes calls in python class. Where if method is not present it is required to perform some task. Here in the example I have written some methods and some calls.

What I want to achieve is, if any call is not defined in the class, it goes to __getattr__, and there I want to check if a method by the name "get_{the_called_method_or_attribute}" is present or not. If present just call the method and return its value.

I tried using dir, self__dict__ and other, but none worked. It just called itself in recursion.

Is there any way to perform my query by not checking the existance of method or attribute in the call section rather inside the class?

class C:

    def __init__(self, data):
        self.data = data

    def get_name(self):
        return "{f_name} {l_name}".format(**self.data)

    def get_f_name(self):
        return data.get("f_name")

    def get_l_name(self):
        return data.get("l_name")

    def get_email(self):
        return data.get("email")

    def access_undefined_methods(self, key):
        return self.data.get(key, "Key '{}' does not present".format(key))

    def __getattr__(self, name):
        return self.access_undefined_methods(name)

data = {"f_name": "John", "l_name": "Doe", "email": "john@doe.com"}
obj = C(data)
print obj.get_name()    # <-- John Doe
print obj.f_name        # <-- John
print obj.l_name        # <-- Doe
print obj.email         # <-- john@doe.com
print obj.name          # <-- this should execute obj.get_name()
IT.dumb
  • 71
  • 6
  • Can you explain why you want to do this? I suspect this is an [XY Problem](https://meta.stackexchange.com/a/66378/344593) – TemporalWolf Mar 14 '17 at 18:48
  • I think there is too much magic in doing that. Can you explain what you want to achieve? Maybe there's an alternative solution. – nir0s Mar 14 '17 at 18:57
  • 1
    Actually there is no problem to be solved. I just wanted to know if that is possible. If I had checked the existence of method during call time, it would be possible. But with this code setup I was unable to perform so. So I placed a question here. My question still is, is this possible or not? – IT.dumb Mar 15 '17 at 02:24

1 Answers1

0

You can check whether the get_XXX method exists by using the dir() function to get a dictionary of the object's methods.

def access_undefined_methods(self, name):
    if 'get_' + name in dir(self):
        return dir(self)['get_' + name'])(self)
    else:
        return "Key '{}' does not present".format(key)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Did you try the code? Or did you read my question? I said using `dir` did not work. the output is ```File "test.py", line 78, in __getattr__ return self.access_undefined_methods(name) File "test.py", line 72, in access_undefined_methods if 'get_' + name in dir(self): File "test.py", line 78, in __getattr__ return self.access_undefined_methods(name) File "test.py", line 72, in access_undefined_methods if 'get_' + name in dir(self): RuntimeError: maximum recursion depth exceeded while calling a Python object'``` – IT.dumb Mar 15 '17 at 02:18