嵌套方法中的属性错误

时间:2014-04-26 01:22:05

标签: python

我有以下课程:

class Connection(object):
    defaults = {
        'application_key':None,
    }


    def __init__(self, application_key="None"):
        self.application_key = application_key or Connection.defaults.get('application_key')

    def make_request(self, params=None, method=None):
        url = URL_FORMAT % {
            'params': params,
        }

        headers = {
            'Authorization': 'Token token='+ self.application_key,
            'content-type': 'application/json',
        }

        if method == "get":
            request = requests.get(url, headers=headers)

            if request.status_code == requests.codes.ok:
            return request.json()
            else:
                raise APIError("%s status received (not 200)" % request.status_code)
        elif method == "post":
            request = requests.post(url, headers=headers)
            request.status_code == requests.codes.ok
            if request.status_code == requests.codes.ok:
                return request.json()
            else:
                raise APIError("%s status received (not 200)" % request.status_code)


    def get_geofence(self):
        try:
            data = self.make_request('geofences', 'get')

            return data

        except APIError:
            raise GeofenceNotFound("Geofence not found")

    def get_geofence_id(self, geofence_id=None):
        try:
            data = self.make_request('geofences/'+self.geofence_id+'/', 'get')

            return data

        except APIError:
            raise GeofenceNotFound("Geofence not found with id #%s" % self.geofence_id)

问题行似乎是data = self.make_request('geofences/'+self.geofence_id+'/', 'get')返回AttributeError: 'Connection' object has no attribute 'geofence_id'

我在这里很难过。

1 个答案:

答案 0 :(得分:3)

geofence_id不是类属性,它是一个函数参数。因此,您应该引用geofence_id而不是self.geofence_id