diff --git a/Python/1-example.py b/Python/1-example.py new file mode 100644 index 0000000..8dadc46 --- /dev/null +++ b/Python/1-example.py @@ -0,0 +1,34 @@ +class Mixin: + """ + Mixin class + """ + par = 0 + + def outputMethod(self): + print('Mixin method') + +class Test1(Mixin): + """ + Test1 has its own functions and Mixin's class functional + """ + def meth1(self): + print('First method') + +class Test2(Test1, Mixin): + """ + Test2 has its own functions, Test1 and Mixin's class functional + """ + def meth2(self): + print('Second method') + + +instance1 = Test1() +instance1.meth1() +instance1.outputMethod() + +print("") + +instance2 = Test2() +instance2.meth1() +instance2.meth2() +instance2.outputMethod() diff --git a/Python/2-order_ex1.py b/Python/2-order_ex1.py new file mode 100644 index 0000000..6ce773b --- /dev/null +++ b/Python/2-order_ex1.py @@ -0,0 +1,16 @@ +class Mixin(object): + def test(self): + return 'Mixin' + +class Class1(Mixin): + pass + +class Class2(Mixin): + def test(self): + return 'Class2' + +class MainClass(Class1,Class2): + pass + +# order: MainClass -> Class1 -> Class2 -> Mixin +print(MainClass().test()) # Class2 diff --git a/Python/2-order_ex2.py b/Python/2-order_ex2.py new file mode 100644 index 0000000..45884d4 --- /dev/null +++ b/Python/2-order_ex2.py @@ -0,0 +1,23 @@ +class BaseClass(object): + def test(self): + return 'BaseClass' + +class Mixin1(object): + def test(self): + return 'Mixin1' + +class Mixin2(object): + def test(self): + return 'Mixin2' + +class MyClass1(BaseClass, Mixin1, Mixin2): + pass + +class MyClass2(Mixin2, Mixin1, BaseClass): + pass + +# order: MyClass1 -> Mixin2 -> Mixin1 -> BaseClass +print(MyClass1().test()) # BaseClass + +# order: MyClass2 -> BaseClass -> Mixin1 -> Mixin2 +print(MyClass2().test()) # Mixin2