From 5aef4ae8f0e50556102eda5a531f5f05f23bb88d Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Wed, 10 Jan 2018 13:45:18 +0200 Subject: [PATCH 1/2] Basic mixins in python. --- Python/1-example.py | 35 +++++++++++++++++++++++++++++++++++ Python/2-order_ex1.py | 16 ++++++++++++++++ Python/2-order_ex2.py | 23 +++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 Python/1-example.py create mode 100644 Python/2-order_ex1.py create mode 100644 Python/2-order_ex2.py diff --git a/Python/1-example.py b/Python/1-example.py new file mode 100644 index 0000000..ee6d688 --- /dev/null +++ b/Python/1-example.py @@ -0,0 +1,35 @@ +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..4504c5c --- /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 \ No newline at end of file diff --git a/Python/2-order_ex2.py b/Python/2-order_ex2.py new file mode 100644 index 0000000..db6df2b --- /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 \ No newline at end of file From fe9ad01a42b1d14e0a9645dd992e3edafd4db18f Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Thu, 11 Jan 2018 09:10:19 +0200 Subject: [PATCH 2/2] New line added. --- Python/1-example.py | 1 - Python/2-order_ex1.py | 2 +- Python/2-order_ex2.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Python/1-example.py b/Python/1-example.py index ee6d688..8dadc46 100644 --- a/Python/1-example.py +++ b/Python/1-example.py @@ -32,4 +32,3 @@ def meth2(self): instance2.meth1() instance2.meth2() instance2.outputMethod() - diff --git a/Python/2-order_ex1.py b/Python/2-order_ex1.py index 4504c5c..6ce773b 100644 --- a/Python/2-order_ex1.py +++ b/Python/2-order_ex1.py @@ -13,4 +13,4 @@ class MainClass(Class1,Class2): pass # order: MainClass -> Class1 -> Class2 -> Mixin -print(MainClass().test()) # Class2 \ No newline at end of file +print(MainClass().test()) # Class2 diff --git a/Python/2-order_ex2.py b/Python/2-order_ex2.py index db6df2b..45884d4 100644 --- a/Python/2-order_ex2.py +++ b/Python/2-order_ex2.py @@ -20,4 +20,4 @@ class MyClass2(Mixin2, Mixin1, BaseClass): print(MyClass1().test()) # BaseClass # order: MyClass2 -> BaseClass -> Mixin1 -> Mixin2 -print(MyClass2().test()) # Mixin2 \ No newline at end of file +print(MyClass2().test()) # Mixin2