子类化的奇怪问题?

2024-07-08 14:47:51 发布

您现在位置:Python中文网/ 问答频道 /正文

让我们用一个表示Django控制器的类,其中一个方法称为_onSuccess

class ConfirmController(object):
    ...
    def _onSuccess(self, controller):
      ...

该类稍后将实例化为:

^{pr2}$

我正在尝试使用ConfirmController的子类:

class ConfirmControllerEx(ConfirmController):
    def _onSuccess(self, controller):
        # shortened to demonstrate even simple call to super
        # causes a different behaviour
        super(ConfirmControllerEx, self)._onSuccess(controller)

我可能在python学习中错过了一些东西,但是谁能解释一下为什么上面的子类_onSuccess与原始方法等价? 如果我确实使用了上面的子类ConfirmControllerEx

def credit_confirm_info(request, payment_module, template='/some/template.html'):
    controller = ConfirmControllerEx(request, payment_module)
    controller.confirm()   # this method calls self._onSuccess
    return controller.response
credit_confirm_info = never_cache(credit_confirm_info)

我得到了NoneType has no method has_header错误,就像credit_confirm_info被再次调用,但是request参数等于None。在

我希望子类和子类方法_onSuccess与对super的普通调用不会与原始方法不同。我是不是少了点什么?在

更新(异常回溯):

    Traceback:
File "/home/dunric/Projects/Example.com/satchmo/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/dunric/Projects/Example.com/satchmo/gastroceny_cz/localsite/views.py" in cod_confirm_info
  279.             template='shop/checkout/cod/confirm.html')
File "/home/dunric/Projects/Example.com/satchmo/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  90.         add_never_cache_headers(response)
File "/home/dunric/Projects/Example.com/satchmo/lib/python2.7/site-packages/django/utils/cache.py" in add_never_cache_headers
  129.     patch_response_headers(response, cache_timeout=-1)
File "/home/dunric/Projects/Example.com/satchmo/lib/python2.7/site-packages/django/utils/cache.py" in patch_response_headers
  119.     if not response.has_header('Last-Modified'):

    Exception Type: AttributeError at /checkout/cod/confirm/
    Exception Value: 'NoneType' object has no attribute 'has_header'

Tags: infocomcachehomeexampleresponse子类confirm
1条回答
网友
1楼 · 发布于 2024-07-08 14:47:51

我不知道django的具体情况,但是这个方法:

def _onSuccess(self, controller):
    # shortened to demonstrate even simple call to super
    # causes a different behaviour
    super(ConfirmControllerEx, self)._onSuccess(controller)

与父类的_onSuccess等价。它通过super调用父实现,但它忽略该调用返回的任何内容,只返回None(隐式地,通过执行到达方法定义的末尾)。如果您稍后得到一个错误,它似乎表明您有一个None对象(NoneType)的实例,那么这就是我对错误的猜测。但是,如果_onSuccess方法的约定总是返回None,那就不可能了。在

相关问题 更多 >

    热门问题