0

我试图封装一个对象而不改变它的默认行为,问题是当我访问它已经构建的对象时。因此,我的想法是使用继承和使用、、、和__getattr__委托__setattr__给封装的对象,除了我自己定义的字段。__getitem____setitem__

class Request(HttpRequest):

    def __init__(self, request, *args, **kwargs):
        self._original_request = request
        super(Request, self).__init__(*args, **kwargs)

    def __getattr__(self, name):
        return getattr(self._original_request, name)

    def __setattr__(self, name, value):
        if name == '_original_request':
            super(Request, self).__setattr__(name, value)
        else:
            self._original_request.__setattr__(name, value)

    def __getitem__(self, key):
        return self._original_request[key]

    def __setitem__(self, key, value):
        self._original_request[key] = value

如果我传递原始对象,一切都很好。如果我不这样做,我会得到

host = self.META['SERVER_NAME']
KeyError: u'SERVER_NAME'

我错过了什么吗?我的班级不应该表现得与它封装的完全一样吗?

Internal Server Error: /
Traceback (most recent call last):
  File "c:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "c:\Python27\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\www\lookwanted\djutils\views.py", line 35, in dispatch
    return super(View, self).dispatch(Request(request), *args, **kwargs)
  File "C:\www\lookwanted\djutils\views.py", line 12, in __init__
    super(Request, self).__init__(*args, **kwargs)
  File "c:\Python27\lib\site-packages\django\http\request.py", line 43, in __init__
    self.GET, self.POST, self.COOKIES, self.META, self.FILES = {}, {}, {}, {}, {}
  File "C:\www\lookwanted\djutils\views.py", line 21, in __setattr__
    self._original_request.__setattr__(name, value)
AttributeError: can't set attribute
Traceback (most recent call last):
  File "c:\Python27\lib\wsgiref\handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "c:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", line 72, in __call__
    return self.application(environ, start_response)
  File "c:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 255, in __call__
    response = self.get_response(request)
  File "c:\Python27\lib\site-packages\django\core\handlers\base.py", line 178, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "c:\Python27\lib\site-packages\django\core\handlers\base.py", line 217, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "c:\Python27\lib\site-packages\django\views\debug.py", line 69, in technical_500_response
    html = reporter.get_traceback_html()
  File "c:\Python27\lib\site-packages\django\views\debug.py", line 298, in get_traceback_html
    return t.render(c)
  File "c:\Python27\lib\site-packages\django\template\base.py", line 140, in render
    return self._render(context)
  File "c:\Python27\lib\site-packages\django\template\base.py", line 134, in _render
    return self.nodelist.render(context)
  File "c:\Python27\lib\site-packages\django\template\base.py", line 830, in render
    bit = self.render_node(node, context)
  File "c:\Python27\lib\site-packages\django\template\debug.py", line 74, in render_node
    return node.render(context)
  File "c:\Python27\lib\site-packages\django\template\defaulttags.py", line 284, in render
    return nodelist.render(context)
  File "c:\Python27\lib\site-packages\django\template\base.py", line 830, in render
    bit = self.render_node(node, context)
  File "c:\Python27\lib\site-packages\django\template\debug.py", line 74, in render_node
    return node.render(context)
  File "c:\Python27\lib\site-packages\django\template\debug.py", line 84, in render
    output = self.filter_expression.resolve(context)
  File "c:\Python27\lib\site-packages\django\template\base.py", line 578, in resolve
    obj = self.var.resolve(context)
  File "c:\Python27\lib\site-packages\django\template\base.py", line 728, in resolve
    value = self._resolve_lookup(context)
  File "c:\Python27\lib\site-packages\django\template\base.py", line 779, in _resolve_lookup
    current = current()
  File "c:\Python27\lib\site-packages\django\http\request.py", line 112, in build_absolute_uri
    self.get_host(), self.path)
  File "c:\Python27\lib\site-packages\django\http\request.py", line 62, in get_host
    host = self.META['SERVER_NAME']
KeyError: u'SERVER_NAME'
4

2 回答 2

2

问题出在这条线上:

    super(Request, self).__init__(*args, **kwargs)

这将导致在您的实例上调用该类的__init__方法,这是没有意义的。因此,只需摆脱那条线。HttpRequestRequest

另外,我不明白为什么您首先要进行子类化。由于 python 有鸭子类型,并且你完全包装了内部对象,你为什么不去:

class Request(object):
    def __init__(self, request, *args, **kwargs):
        self._original_request = request

    def __getattr__(self, name):
        return getattr(self._original_request, name)

    def __setattr__(self, name, value):
        if name == '_original_request':
            super(Request, self).__setattr__(name, value)
        else:
            self._original_request.__setattr__(name, value)

    def __getitem__(self, key):
        return self._original_request[key]

    def __setitem__(self, key, value):
        self._original_request[key] = value
于 2013-06-10T17:45:41.877 回答
0

堆栈跟踪显示了问题:代码试图将属性设置为 onself._original_request而不是self.__dict__.

阅读自定义属性访问的 Python 文档。

于 2013-06-10T17:39:59.563 回答