Python3 - 从字符串调用私有方法

时间:2018-06-18 10:48:09

标签: python-3.x oop

我有这个课程:

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

但是,一旦我实例化:

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

它显示了错误: AttributeError: 'APImanager' object has no attribute '__get'

这是通过方法字符串名称执行私有方法的任何其他方法吗?

0 个答案:

没有答案