简体   繁体   中英

Multi inheritance and design pattern

In the facebook-python-business-sdk library, I've seen something that I thought it was bad practice :

AdAccountUserMixin 's get_pages() method uses self.iterate_edge() which is neither a method of this class nor a parent's.

AbstractCrudObject implements this method.

And then, AdAccountUser inherits from these two classes. That's why an object of AdAccountUser can use the method get_pages().

Minimal example :

class AbstractBar:
   def bar(self, x):
       return x

class Foo:
   def foo(self, x):
       return self.bar(x)

class Test(Foo, AbstractBar):
   def test(self, x):
       return self.foo(x)


t = Test()

t.test(5) # returns 5

Is this a design pattern, something that you see everyday or just a bad practice ?

That's quite ordinary mixin class stuff. A mixin class is a class that is explicitely designed to complement a given base class or interface (it explicitely relies on the class it's "mixed-in" with to implement a given interface). This is quite useful when you want to factor out some common behaviour from a bunch of classes that implement the same interface without having a common ancestor.

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