从类中调用常规函数?

时间:2017-02-17 17:58:56

标签: python

我是Python的新手,我有工厂类,它接受API参数,我的设置得到一个NameError:'self'没有定义。我明白我的函数调用是错误的。我如何重构这个具有相同的逻辑

import requests
import json

class Call:

    results = []
    __response = None
    __methods= dict(post=self.__post(), get=self.__get())


    def __init__(self, root, endpoint, payload, header):
        self.__root = root
        self.__endpoint = endpoint
        self.__payload = payload
        self.__header = header

    def __post(self):

       self.__response = requests.post(
            self.__root + self.__endpoint, 
            data = json.dumps(self.__payload), 
            headers = self.__header
        )

       self.__get_results()

    def __get(self):

       self.__response = requests.get(
            self.__root + self.__endpoint, 
            data = json.dumps(self.__payload), 
            headers = self.__header
        )

       self.__get_results()

    def __get_results(self):

        if (self.__response.ok):
            data = json.loads(self.__response.content)
            results.append(
                {
                    'result':data['result'], 
                    'endpoint': self.__endpoint, 
                    'status' : response.status_code
                }
            ) 
        else:
            results.append(
                {
                    'result':'FAILED', 
                    'endpoint': self.__endpoint, 
                    'status' : response.status_code
                }
            )

    def method(self, method):
        return self.__methods[method]

login = Call(
    Url.V1_PROD, 
    DriverEndpoint.LOGIN, 
    DriverPayload.LOGIN, 
    Request.HEADER
)

login.method('post')

2 个答案:

答案 0 :(得分:2)

您不应该使用以__double __underscore开头的变量名称,它们用于调用您可能不想要的名称修改。 使用_single _underscore。

使用__init__方法在实例上声明字典更容易。 一种常见的替代方法是将要调用的方法的名称存储为字符串,并使用getattr来访问方法(请参阅Call a Python method by name)。

import requests
import json

class Call:

    results = []
    _response = None


    def __init__(self, root, endpoint, payload, header):
        self._root = root
        self._endpoint = endpoint
        self._payload = payload
        self._header = header
        # NO () after self._post, otherwise it would call the
        # method and insert the return value in the dict
        self._methods= dict(post=self._post, get=self._get)

    def _post(self):

       self._response = requests.post(
            self._root + self._endpoint, 
            data = json.dumps(self._payload), 
            headers = self._header
        )

       self._get_results()

    def _get(self):

       self._response = requests.get(
            self._root + self._endpoint, 
            data = json.dumps(self._payload), 
            headers = self._header
        )

       self._get_results()

    def _get_results(self):

        if (self.__response.ok):
            data = json.loads(self.__response.content)
            results.append(
                {
                    'result':data['result'], 
                    'endpoint': self._endpoint, 
                    'status' : response.status_code
                }
            ) 
        else:
            results.append(
                {
                    'result':'FAILED', 
                    'endpoint': self._endpoint, 
                    'status' : response.status_code
                }
            )



    def method(self, method):
        # Here, we have to effectively call the selected method,
        # hence the () at the end
        self._methods[method]()

login = Call(
    Url.V1_PROD, 
    DriverEndpoint.LOGIN, 
    DriverPayload.LOGIN, 
    Request.HEADER
)

login.method('post')

答案 1 :(得分:0)

__methods= dict(post=self.__post(), get=self.__get())

您的__methods变量是类变量,self用于引用Call的实例,self无法访问类变量范围。

您可以将__method声明为实例变量:

def __init__(self):
    self.__methods= dict(post=self.__post(), get=self.__get())

def get_mothods(self):
    return self.__methods