简体   繁体   中英

Class Inheritence Naming in Python

There is a 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. 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.

Option 1: do not implement create in class C

If you don't implement method create in C , then Base1.create is going to be used.

Note that this situation where C inherits from Base1 and Base2 is treated as if C inherites from Base1 and Base1 inherits from Base2 .

You can see that if you print C.__mro__

See also this thread about MRO: Method Resolution Order (MRO) in new style Python classes

Option 2: do not call the base implemntation

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

Now Base1.create is no longer going to be called.

Option 3: call only one of the bases

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

Now Base1.create is not going to be called, but Base2.create is.

Option 4: call each of the base implementations

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

Both Base1.create and Base2.create will be called.

Option 5: user super to call all base implementations

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. So, an alternative approach is to user super , which uses the MRO (see Option 1) to determine which base implementation to use. By using MRO, it avoids diamond inheritance problems. 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.

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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