mirror of
https://github.com/Hizenberg469/Python-tutorial.git
synced 2026-04-19 23:12:24 +03:00
OOP finished
This commit is contained in:
32
OOP/MRO.py
Normal file
32
OOP/MRO.py
Normal file
@@ -0,0 +1,32 @@
|
||||
#MRO - Method Resolution order
|
||||
|
||||
class A:
|
||||
num = 10
|
||||
|
||||
class B(A):
|
||||
pass
|
||||
|
||||
class C(A):
|
||||
num = 1
|
||||
|
||||
class D(B,C):
|
||||
pass
|
||||
|
||||
print(D.mro())
|
||||
|
||||
# [<class '__main__.D'>,
|
||||
# <class '__main__.B'>,
|
||||
# <class '__main__.C'>,
|
||||
# <class '__main__.A'>,
|
||||
# <class 'object'>]
|
||||
|
||||
# The order of this determined using DFS.
|
||||
|
||||
# A
|
||||
# / \
|
||||
# / \
|
||||
# B C
|
||||
# \ /
|
||||
# \ /
|
||||
# D
|
||||
|
||||
18
OOP/SuperList.py
Normal file
18
OOP/SuperList.py
Normal file
@@ -0,0 +1,18 @@
|
||||
#Implementing List:
|
||||
|
||||
class SuperList(list):
|
||||
def __len__(self):
|
||||
return 1000
|
||||
|
||||
|
||||
super_list1 = SuperList()
|
||||
|
||||
print(len(super_list1))
|
||||
|
||||
super_list1.append(5)
|
||||
|
||||
print(super_list1[0])
|
||||
|
||||
print(issubclass(SuperList, list))
|
||||
|
||||
print(issubclass(list, object))
|
||||
37
OOP/game.py
Normal file
37
OOP/game.py
Normal file
@@ -0,0 +1,37 @@
|
||||
class User(object):
|
||||
def __init__(self, email):
|
||||
self.email = email
|
||||
print('init complete')
|
||||
|
||||
def sign_in(self):
|
||||
print('logged in')
|
||||
|
||||
class Wizard(User):
|
||||
def __init__(self, name, power):
|
||||
self.name = name
|
||||
self.power = power
|
||||
|
||||
def attack(self):
|
||||
print(f'attacking with power of {self.power}')
|
||||
|
||||
class Archer(User):
|
||||
def __init__(self, name, arrows):
|
||||
self.name = name
|
||||
self.arrows = arrows
|
||||
|
||||
def check_arrows(self):
|
||||
print(f'{self.arrows} remaining')
|
||||
|
||||
def run(self):
|
||||
print('ran really fast')
|
||||
|
||||
class HybridBorg(Wizard, Archer):
|
||||
def __init__(self, name, power, arrows):
|
||||
Archer.__init__(self, name, arrows)
|
||||
Wizard.__init__(self, name, power)
|
||||
|
||||
hb1 = HybridBorg('borgie', 50, 100)
|
||||
|
||||
print(hb1.check_arrows())
|
||||
print(hb1.attack())
|
||||
print(hb1.sign_in())
|
||||
47
OOP/wiki.txt
47
OOP/wiki.txt
@@ -109,4 +109,49 @@
|
||||
It specify what are the functions and variable the instance of
|
||||
a class(object) has access to.
|
||||
|
||||
-> Dunder methods:
|
||||
-> Dunder methods:
|
||||
|
||||
Dunder methods allow us to modify and implement built-in function
|
||||
of objects. This allow us to implement our own custom logic for
|
||||
built-in function which work for objects. The modification only
|
||||
works for that same object only and if we apply it on different
|
||||
object then it would behave as usual(or as implemented for other
|
||||
object)
|
||||
|
||||
|
||||
for ex:
|
||||
|
||||
class Toy():
|
||||
def __init__(self, color, age):
|
||||
self.color = color
|
||||
self.age = age
|
||||
self.my_dict = {
|
||||
'name' : 'Yoyo',
|
||||
'has_pets' : False
|
||||
}
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.color}'
|
||||
|
||||
def __len__(self):
|
||||
return 5
|
||||
|
||||
def __call__(self):
|
||||
return ('yes??')
|
||||
|
||||
def __getitem__(self, i):
|
||||
return self.my_dict[i]
|
||||
|
||||
|
||||
action_figure = Toy('red',0)
|
||||
print(action_figure.__str__())
|
||||
print(str(action_figure))
|
||||
print(len(action_figure))
|
||||
print(action_figure())
|
||||
print(action_figure['name'])
|
||||
|
||||
-> MRO (Method Resolution order):
|
||||
|
||||
MRO allow us to determine the order in which
|
||||
the Inheritance need to be processed. This
|
||||
order is determined using Depth first search.
|
||||
Reference in New Issue
Block a user