我不明白的Python3 AttributeError

时间:2019-05-29 13:00:16

标签: python python-3.x error-handling

底部的代码在我的程序中生成以下回溯。

Traceback (most recent call last):                                                                                                         │
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1832, in full_dispatch_request                                          │
    rv = self.dispatch_request()                                                                                                           │
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1818, in dispatch_request                                               │
    return self.view_functions[rule.endpoint](**req.view_args)                                                                             │
  File "/usr/local/lib/python3.6/site-packages/flask_restplus/api.py", line 325, in wrapper                                                │
    resp = resource(*args, **kwargs)                                                                                                       │
  File "/usr/local/lib/python3.6/site-packages/flask/views.py", line 87, in view                                                           │
    self = view.view_class(*class_args, **class_kwargs)                                                                                    │
  File "/etc/changes/api/__init__.py", line 22, in __init__                                                                                │
    self.request_event = RequestEvent(request.environ)                                                                                     │
  File "/etc/changes/event/request_event.py", line 11, in __init__                                                                         │
    super(RequestEvent, self).__init__(event_data)                                                                                         │
  File "/etc/changes/event/__init__.py", line 14, in __init__                                                                              │
    self._load_json_object(event_data)                                                                                                     │
  File "/etc/changes/event/request_event.py", line 19, in _load_json_object                                                                │
    self.client_cert = self._get_certification_details().get('client_serial', None)                                                        │
  File "/etc/changes/event/request_event.py", line 23, in _get_certification_details                                                       │
    CLIENT_VERIFY = self.request_environment.get('CLIENT_VERIFY', None)                                                                    │
AttributeError: 'RequestEvent' object has no attribute 'request_environment' 

我不明白为什么,因为属性显然是在方法之前进行的。有什么线索可以帮助我吗?为了清楚起见,一些代码已被删除。

from event import Event
from manager.manager_factory import ManagerFactory

class RequestEvent(Event):


    def __init__(self, request_environment, event_data={}):
        super(RequestEvent, self).__init__(event_data)
        self.request_environment = request_environment
        self.certification_details = self._get_certification_details()

    def _load_json_object(self,json_obj):
        super()._load_json_object(json_obj)
        self.type = 'request'
        self.trigger_invocation = json_obj.get('trigger_invocation', False)
        self.client_cert = self._get_certification_details().get('client_serial', None)
        self.tenant = ManagerFactory().authentication_manager

    def _get_certification_details(self):
        CLIENT_VERIFY = self.request_environment.get('CLIENT_VERIFY', None)
                   client_certificate_details = {}
        client_certificate_details['client_verify'] = CLIENT_VERIFY
        return client_certificate_details

1 个答案:

答案 0 :(得分:1)

仔细检查回溯。

您在那里有一个递归定义。超级__init__正在调用子类的_load_json_object方法。依次调用_get_vertification_details方法。所有这些都是在定义request_environment属性之前发生的。