简体   繁体   English

如何使用 python 中的子 class 的方法调用第三个或更高的父 class 的方法?

[英]How to call method of third or higher parent class using method of child class in python?

I am trying to develop a RESTful API by means of Django RestFramework.我正在尝试通过 Django RestFramework 开发一个 RESTful API。 I need to call the validation method (say method a) of a class (say RawUser) in subclasses.我需要在子类中调用 class(比如 RawUser)的验证方法(比如方法 a)。 For the first two levels "super()" can help, but I do not know the solution for the deeper levels.对于前两个级别,“super()”可以提供帮助,但我不知道更深层次的解决方案。 This is my sample code:这是我的示例代码:

class RawUser:
    def a(self):
        return 'something'


class AnonymousUser(RawUser):
    def a(self):
        return super().a()


class RegisteredUser(AnonymousUser):
    def a(self):
        return super(AnonymousUser, self).a()


class VIPUser(RegisteredUser):
    def a(self):
        # How can I call method "a" of RawUser here?


class SpecialUser(VIPUser):
    def a(self):
        # How can I call method "a" of RawUser here?


class AdminUser(SpecialUser):
    def a(self):
        # How can I call method "a" of RawUser here?

I need to call the method "a" of RawUser in deep subclasses for example AdminUser.我需要在深层子类中调用 RawUser 的方法“a”,例如 AdminUser。 How can I do this?我怎样才能做到这一点?

RawUser.a(instance)

When you call instance.method() you are actually calling Class.method(instance), the self is actually an argument.当您调用 instance.method() 时,实际上是在调用 Class.method(instance),self 实际上是一个参数。

You can do the same thing you already do in RegisteredUser .您可以在RegisteredUser中做同样的事情。

Just use super(AnonymousUser, self).a() in every class except AnonymousUser and RawUser.只需在除 AnonymousUser 和 RawUser 之外的每个 class 中使用super(AnonymousUser, self).a() This will only call RawUser and the class you're in. I've tested it here这只会调用 RawUser 和你所在的 class。我在这里测试过

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中使用子类的方法调用第二个父类的方法? - How to call method of second parent class using method of child class in python? 如何在Python的子类中调用和覆盖父类方法 - How to call and override a Parent class method from Child class in Python Python父/子类方法调用 - Python Parent/Child class method call Python 如何从父元类调用子 class 方法 - Python How to call child class method from Parent metaclass 在Python 2中从子类调用父类方法 - Call a parent class method from a child class in Python 2 在python中用子类对象调用父类的重写方法 - Call overridden method of parent class with child class object in python 在继承的情况下,是否可以通过在python中使用子类对象引用来调用第二个父类方法? 如果是,那怎么办? - Is it possible to call 2nd parent class method by using child class object reference inside python in case of inheritance? If yes then how? 在Python2中,如何强制子类方法调用父方法而不明确要求最终用户包含它? - In Python2, how to force a child class method to call a parent method without explicitly requiring the end user to include it? 如何从 Python 中的子类调用父类的方法? - How do I call a parent class's method from a child class in Python? 我们可以在python 2.7中从子类的类方法中调用父类的实例方法吗? - Can we call instance method of parent class from class method of child class in python 2.7?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM