0

I have this class:

class APImanager:

    def request(self, r_type, **params):
        # validate if given request type is valid or not.
        available = ["get", "post", "put", "patch", "delete"]
        if r_type not in available:
            print("The given request type '{}' is not valid.".format(r_type))

        return getattr(self, "__{}".format(r_type))(**params)

    def __get(self, uri="", headers=None) -> str:
        """
        Get method. This will set automatically the header

        :param uri: The uri to get
        :type uri: str
        """

        u = self.url + uri
        return u

But, once I instantiates with:

api = APImanager()
print(api.request("get", uri="projects"))

It shows me the error: AttributeError: 'APImanager' object has no attribute '__get'.

Is that any other way to execute private methods by method string name?

fiskolin
  • 1,421
  • 2
  • 17
  • 36
  • Please read the accepted answer for the dup question carefully, especially the part about leading double underscores and name mangling – DeepSpace Jun 18 '18 at 10:54
  • The method is *class private*, because it is meant to be used within a single class only, protecting it from accidental clashes with the same name in subclasses. As such the name is automatically *namespaced to the class*, and you have to manually apply the same name change. See the duplicate. – Martijn Pieters Jun 18 '18 at 11:00

0 Answers0