Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh618 authored Sep 3, 2020
1 parent e230780 commit eca11c3
Show file tree
Hide file tree
Showing 67 changed files with 1,095 additions and 0 deletions.
31 changes: 31 additions & 0 deletions OOP/@[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 11:44:06 2020
@author: saura
"""
class PlayerCharacter:
membership = True
def __init__(self, name, age):
if self.membership :
self.name = name
self.age = age

def run(self):
print(f"run {self.name}")

@classmethod # we can use this method without instantiating the class
def add_things(cls, n1, n2):
return cls('Jojo', n1+n2)

@staticmethod # same as @classmethod, only thing is we don't pass the class as argument, and hence can't use its attribute
def add_things2(n1, n2):
return n1+n2

player1 = PlayerCharacter.add_things(4,5)
print(player1.name)
print(player1.age)
print(player1.membership)

print(PlayerCharacter.add_things2(45, 5))
# print(player2.membership) # gives error
23 changes: 23 additions & 0 deletions OOP/OOP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 09:34:05 2020
@author: saura
"""
#OOP
class BigObject:
pass

obj1 = BigObject()
obj2 = BigObject()
obj3 = BigObject()

print(type(None))
print(type(True))
print(type(5))
print(type(5.5))
print(type('hi'))
print(type([]))
print(type(()))
print(type({}))
print(type(obj1))
36 changes: 36 additions & 0 deletions OOP/class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 10:56:03 2020
@author: saura
"""
class PlayerCharacter:
def __init__(self, name, age):
self.name = name # Attribute or Property
self.age = age
print("I get printed at every instance created")
def run(self):
print("run method")

player1 = PlayerCharacter("Rohan", 22)
player2 = PlayerCharacter("Mohan", 98)
player2.attack = True # creating a new attribute for the player2

print(player1) # Memory location of the object
print(player1.name) # Notice that we are accessing the attributes, and hence we are not using the curly brackets at the end
print(player1.age)

print(player2)
print(player2.name)
print(player2.age)

print(player1.run()) # Here we are accessing the method, and hence using curly brackets

print(player2.attack)

'''
Note: Self refers to the object itself. So, self.name is nothing but player1.name
this way by using self, we pass the object itself to the class.
Every class method should have first argument as 'self'
and first method should be __init__ method.
'''
23 changes: 23 additions & 0 deletions OOP/class_attribute_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 11:16:03 2020
@author: saura
"""
class PlayerCharacter:
membership = True # Class object attribute, its an attribute of PlayerCharacter. It is a static attribute
def __init__(self, name, age):
if self.membership : # or we can write: 'if PlayerCharacter.membership :'
self.name = name # these are dynamic attribute
self.age = age

def run(self):
print(f"run {self.name}")
# print(f"run {PlayerCharacter.name}) # we cannot do this

player1 = PlayerCharacter("Rohan", 22)
player2 = PlayerCharacter("Mohan", 98)

print(player1.name)
print(player1.membership) # each of the class instace can assess the class object attribute
print(player1.run())
55 changes: 55 additions & 0 deletions OOP/dunder_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 14:36:41 2020
@author: saura
"""
class ActionFig():
def __init__(self, color, age):
self.color = color
self.age = age
self.my_dict = {"name": "yoyo", "has_pets": "Dog"}

def __str__(self):
return f'{self.color}'

def __len__(self):
return 5

def __call__(self):
return 'hello...??'

def __getitem__(self, i):
return self.my_dict[i]

def __del__(self):
return 'deleted'

hulk = ActionFig("red", 0)

print(hulk.__str__())
print(hulk)
print(str(hulk))
print(str('Printing an string'))

print(hulk.__len__())
print(len(hulk))
print(len('0123456789'))

print(hulk.__call__())
print(hulk()) # with call method, we get this special power to call a method with curly braces

print(hulk.__getitem__("has_pets"))

print(hulk.__repr__()) # it is same as print(hulk), it prints the memory location of the object

print(hulk.__del__())
del hulk # this deletes the variable passed, but as we have modified it, it won't delete the instance now

a = 5
del a
# print(a)
'''
this will give us error, because we have deleted the variable a,
because we haven't modified the base class __del__ method, so its performing normal
'''
46 changes: 46 additions & 0 deletions OOP/exercise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 14:14:52 2020
@author: saura
"""
class Pets():
animals = []
def __init__(self, animals):
self.animals = animals

def walk(self):
for animal in self.animals:
print(animal.walk())

class Cat():
is_lazy = True

def __init__(self, name, age):
self.name = name
self.age = age

def walk(self):
return f'{self.name} is just walking around'

class Simon(Cat):
def sing(self, sounds):
return f'{sounds}'

class Sally(Cat):
def sing(self, sounds):
return f'{sounds}'

#1 Add nother Cat
class Suzy(Cat):
def sing(self, sounds):
return f'{sounds}'

#2 Create a list of all of the pets (create 3 cat instances from the above)
my_cats = [Simon('Simon', 4), Sally('Sally', 21), Suzy('Suzy', 1)]

#3 Instantiate the Pet class with all your cats
my_pets = Pets(my_cats)

#4 Output all of the cats singing using the my_pets instance
my_pets.walk()
21 changes: 21 additions & 0 deletions OOP/extending_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 19:21:12 2020
@author: saura
"""
class SuperList(list):
def __len__(self):
return 1000

super_list1 = SuperList()
print(super_list1)

super_list1.append(0)
super_list1.append(100)
print(super_list1)

print(len(super_list1))

print(issubclass(SuperList, list))
print(issubclass(list, object))
28 changes: 28 additions & 0 deletions OOP/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 13:23:35 2020
@author: saura
"""
class User():
def signed_in(self):
print("User is logged in.")

class Wizard(User):
def __init__(self, name, power):
self.name = name
self.power = power

def attack(self):
print(f"{self.name} is attacking with {self.power} power.")

class Archer(User):
pass

wizard1 = Wizard("John", 50)
archer1 = Archer()

print(wizard1.signed_in())
print(wizard1.attack())

print(archer1.signed_in())
32 changes: 32 additions & 0 deletions OOP/isinstance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 13:33:45 2020
@author: saura
"""
class User():
def signed_in(self):
print("User is logged in.")

class Wizard(User):
def __init__(self, name, power):
self.name = name
self.power = power

def attack(self):
print(f"{self.name} is attacking with {self.power} power.")

class Archer(User):
pass

wizard1 = Wizard("John", 50)

print(isinstance(wizard1,Wizard))
print(isinstance(wizard1,User))
print(isinstance(wizard1,object))
print(isinstance(wizard1,Archer))

'''
By default every class is a subclass of 'object' class, hence when we type 'instance.' (object_name dot), we get all those
defualt methods suggestions from the IDE.
'''
26 changes: 26 additions & 0 deletions OOP/mro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 20:43:40 2020
@author: saura
"""
# Method Resolution Order
# it tells the order of the preferences for classes

class A:
num = 10

class B(A):
pass

class C(A):
num = 1

class D(B,C):
pass

print(D.num)

print(D.mro())
print(D.__mro__)
# both the above are same
16 changes: 16 additions & 0 deletions OOP/mro2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 21:04:09 2020
@author: saura
"""
class X:pass
class Y:pass
class Z:pass
class A(X,Y):pass
class B(Y,Z):pass
class M(B,A,Z):pass

print(M.mro())

# avoid using this in codes, because MRO rules are very confusing to understand.
44 changes: 44 additions & 0 deletions OOP/multiple_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 20:23:27 2020
@author: saura
"""
class User():
def signed_in(self):
print("User is logged in.")

class Wizard(User):
def __init__(self, name, power):
self.name = name
self.power = power

def attack(self):
print(f"{self.name} is attacking with {self.power} power.")

class Archer(User):
def __init__(self, name, arrows):
self.name = name
self.arrows = arrows

def attack(self):
print(f"{self.name} is attacking with {self.arrows} arrows.")

def check_arrows(self):
print(f"{self.arrows} arrows left.")

class HybridAttacker(Wizard, Archer): # notice the order, in that order only it will give preference
def __init__(self, name, power, arrows):
Wizard.__init__(self,name,power)
Archer.__init__(self,name,arrows)

hbot = HybridAttacker("Hydro", 50, 300)
hbot.attack()

print(hbot.name)
print(hbot.power)
print(hbot.arrows)

hbot.check_arrows()

hbot.signed_in()
Loading

0 comments on commit eca11c3

Please sign in to comment.