Skip to content

Basic mixins in python. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Python/1-example.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 16 additions & 0 deletions Python/2-order_ex1.py
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions Python/2-order_ex2.py
Original file line number Diff line number Diff line change
@@ -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