简体   繁体   English

Python中的类继承命名

[英]Class Inheritence Naming in Python

There is a BaseClient 有一个BaseClient

class BaseClient(object):

that later get inherits in a lot of classes 后来得到很多类的继承

class Account(BaseClient):

    def create(self, **params):
        pass

and few others. 和其他几个。

class MainClass(Account, User):
    pass

There a few functions that use the same create function 有一些使用相同创建功能的功能

def create(self, **params):
        pass

How to add a unique class label like 如何添加唯一的类标签,例如

MainClass.Account.create() 

Now it is working as 现在它正在像

MainClass.create()

Update: 更新:
There a lot duplicate functions like create() that going to override the ones that are inherting from. 有很多重复的函数(例如create())将覆盖继承的函数。 I would like to call the class like Account, so when I call 我想像“帐户”这样的课程,所以当我打电话时

MainClass.Account.create()
MainClass.User.create()  

they act so two different functions. 他们扮演两个不同的职能。

In other words, you have multiple inheritance, with: 换句话说,您具有以下多重继承:

class Base1(object):
    def create(self): ...

class Base2(object):
    def create(self): ...

class C(Base1, Base2):
    def create(self): ...

In class C , you can choose whether to call the implementation from the parent classes or not. 在类C ,您可以选择是否从父类中调用实现。

Option 1: do not implement create in class C 选项1:不要在C类中实现create

If you don't implement method create in C , then Base1.create is going to be used. 如果您没有在C实现create方法,那么将使用Base1.create

Note that this situation where C inherits from Base1 and Base2 is treated as if C inherites from Base1 and Base1 inherits from Base2 . 请注意,将C继承自Base1Base2情况视为C继承自Base1Base1继承自Base2

You can see that if you print C.__mro__ 您可以看到,如果您print C.__mro__

See also this thread about MRO: Method Resolution Order (MRO) in new style Python classes 另请参见以下有关MRO的主题: 新式Python类中的方法解析顺序(MRO)

Option 2: do not call the base implemntation 选项2:不调用基本蕴含

class C(Base1, Base2):
    def create(self):
        pass

Now Base1.create is no longer going to be called. 现在不再调用Base1.create

Option 3: call only one of the bases 选项3:仅呼叫基地之一

class C(Base1, Base2):
    def create(self):
        Base2.create(self)

Now Base1.create is not going to be called, but Base2.create is. 现在Base1.create调用Base2.create ,而是Base2.create

Option 4: call each of the base implementations 选项4:调用每个基本实现

class C(Base1, Base2):
    def create(self):
        Base1.create(self)
        Base2.create(self)

Both Base1.create and Base2.create will be called. Base1.createBase2.create都将被调用。

Option 5: user super to call all base implementations 选项5: super用户调用所有基本实现

Although option 4 may seem like a very nice solution here, in some configurations, like diamond inheritance it could cause a method to be called multiple times. 尽管选项4在这里似乎是一个很好的解决方案,但在某些配置中,例如钻石继承,它可能导致方法被多次调用。 So, an alternative approach is to user super , which uses the MRO (see Option 1) to determine which base implementation to use. 因此,对user super ,另一种方法是使用MRO(请参阅选项1)来确定要使用的基本实现。 By using MRO, it avoids diamond inheritance problems. 通过使用MRO,可以避免钻石继承问题。 However, it has to be used systematically on all classes and even then it has its caveats. 但是,必须在所有类上系统地使用它,即使这样也有其警告。

class CommonBase(object):
    def create(self):
        pass

class Base1(CommonBase):
    def create(self):
        super(Base1, self).create()

class Base2(CommonBase):
    def create(self):
        super(Base2, self).create()

class C(Base1, Base2):
    def create(self):
        super(C, self).create()

Here, C().create() will call all four create methods, each once. 在这里, C().create()将调用所有四个create方法,每个方法一次。

You can't control it as a client of the class from the outside of the class, it can only be controlled inside a class, in your case inside MainClass by calling super to call a method from one or another base class: Account or User . 您不能从类外部将其作为类的客户端进行控制,只能在类内部进行控制,在您的情况下, MainClassMainClass内部通过调用super从一个或另一个基类调用方法来控制它: AccountUser

class MainClass(Account, User):
  # your own convention that by default it calls Account.create
  def create(self, **params):
     super(Account, self).create(**params)

  def create2(self, **params):
     super(User, self).create(**params)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM