Python 3调用超类方法

时间:2019-01-29 12:01:33

标签: django python-3.x

我正在用Django开发应用程序。我是python的新手,从子类调用超级调用方法时遇到问题。我正在使用Python 3。

这是我的太阳课:

class TestClass(BaseController):
    def try_base(request: HttpRequest):
        return super().send_response(200, "", "", model1())

这是我的超级班

class BaseController:

    def convert_to_dict(obj):
        return obj.__dict__

    def send_response(self, http_status, error_code, error_message, response_object):
        obj = BaseResponse(http_status, error_code, error_message, response_object)
        data = json.dumps(obj, default=convert_to_dict, indent=4)
        return HttpResponse(data)

我不知道到底是什么问题。它总是给我一个错误

super(type, obj): obj must be an instance or subtype of type

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:3)

您的方法需要将vulnerable作为其第一个参数:

XSS

所有方法都要求self作为第一个参数。

为此,您完全不需要使用class TestClass(BaseController): def try_base(self, request: HttpRequest): return super().send_response(200, "", "", model1()) 。只需直接致电self。仅当您要在基类中调用相同名称的方法时才需要super()

self.send_response(200, "", "", model1())

至于为什么会出现该特定错误,我不确定,因为您的代码没有给我该错误。注释中可能暗示您实际上正在运行旧版本的Python。

在评论讨论中,我认为您必须进行一些设置,以便Django在TestClass实例上调用super()而不是调用class TestClass(BaseController): def try_base(self, request: HttpRequest): return self.send_response(200, "", "", model1()) 。这也解释了为什么TestClass.try_base(obj)对您不起作用:它要求方法的第一个参数将实例作为其第一个参数。除非您有要调用的实例,否则不能调用作为实例方法的try_base()

相关问题