Skip to content

Commit d6ddf5e

Browse files
authored
Merge pull request #1 from levops-cloud/master
pull req
2 parents 30cefd7 + 3cd6e96 commit d6ddf5e

25 files changed

+273471
-104
lines changed

OOP/dog.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def __init__(self, name, age):
1616
self.name = name
1717
self.age = age
1818

19+
def speak(self, sound):
20+
return f"{self.name} says {sound}"
21+
1922

2023
# Instantiate the Dog object
2124
philo = Dog("Philo", 5)
@@ -27,3 +30,6 @@ def __init__(self, name, age):
2730
# Is Philo a mammal?
2831
if philo.species == "mammal":
2932
print(f"{philo.name} is a {philo.species}!")
33+
34+
print(philo.speak("Woof Woof"))
35+
print(mikey.speak("Waf Waf"))

OOP/dog_inheritance.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

OOP/dog_sol.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

OOP/encapsulation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Using OOP in Python, we can restrict access to methods and variables.
2+
# This prevents data from direct modification which is called encapsulation.
3+
# In Python, we denote private attributes using underscore as the prefix i.e single _ or double __.
4+
5+
class Computer:
6+
7+
def __init__(self):
8+
self.__maxprice = 900
9+
10+
def sell(self):
11+
print("Selling Price: {}".format(self.__maxprice))
12+
13+
def setMaxPrice(self, price):
14+
self.__maxprice = price
15+
16+
c = Computer()
17+
c.sell()
18+
19+
# change the price
20+
c.__maxprice = 1000
21+
c.sell()
22+
23+
# using setter function
24+
c.setMaxPrice(1000)
25+
c.sell()

OOP/inheritance.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# parent class
2+
class Bird:
3+
4+
def __init__(self):
5+
print("Bird is ready")
6+
7+
def whoisThis(self):
8+
print("Bird")
9+
10+
def swim(self):
11+
print("Swim faster")
12+
13+
# child class
14+
class Penguin(Bird):
15+
16+
def __init__(self):
17+
# call super() function
18+
super().__init__()
19+
print("Penguin is ready")
20+
21+
def whoisThis(self):
22+
print("Penguin")
23+
24+
def run(self):
25+
print("Run faster")
26+
27+
peggy = Penguin()
28+
peggy.whoisThis()
29+
peggy.swim()
30+
peggy.run()

OOP/polymorphism.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Parrot:
2+
3+
def fly(self):
4+
print("Parrot can fly")
5+
6+
def swim(self):
7+
print("Parrot can't swim")
8+
9+
class Penguin:
10+
11+
def fly(self):
12+
print("Penguin can't fly")
13+
14+
def swim(self):
15+
print("Penguin can swim")
16+
17+
# common interface
18+
def flying_test(bird):
19+
bird.fly()
20+
21+
#instantiate objects
22+
blu = Parrot()
23+
peggy = Penguin()
24+
25+
# passing the object
26+
flying_test(blu)
27+
flying_test(peggy)

closures/closure.func.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def print_msg(msg):
2+
# This is the outer enclosing function
3+
4+
def printer():
5+
# This is the nested function
6+
print(msg)
7+
8+
return printer # returns the nested function
9+
10+
11+
# Now let's try calling this function.
12+
# Output: Hello
13+
another = print_msg("Hello")
14+
another()
15+
16+

closures/closure.insted.class.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def make_multiplier_of(n):
2+
def multiplier(x):
3+
return x * n
4+
return multiplier
5+
6+
7+
# Multiplier of 3
8+
times3 = make_multiplier_of(3)
9+
10+
# Multiplier of 5
11+
times5 = make_multiplier_of(5)
12+
13+
# Output: 27
14+
print(times3(9))
15+
16+
# Output: 15
17+
print(times5(3))
18+
19+
# Output: 30
20+
print(times5(times3(2)))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def print_msg(msg):
2+
# This is the outer enclosing function
3+
4+
def printer():
5+
# This is the nested function
6+
print(msg)
7+
8+
printer()
9+
10+
# We execute the function
11+
# Output: Hello
12+
print_msg("Hello")
13+
14+

decorators/syntax.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This is not a valid Python module - Don't run it.
2+
3+
# ONE DECORATOR
4+
def func(arg1, arg2, ...):
5+
pass
6+
7+
func = decorator(func)
8+
9+
# is equivalent to the following:
10+
11+
@decorator
12+
def func(arg1, arg2, ...):
13+
pass
14+
15+
16+
# TWO DECORATORS
17+
def func(arg1, arg2, ...):
18+
pass
19+
20+
func = deco1(deco2(func))
21+
22+
# is equivalent to the following:
23+
24+
@deco1
25+
@deco2
26+
def func(arg1, arg2, ...):
27+
pass
28+
29+
# DECORATOR WITH ARGUMENTS
30+
def func(arg1, arg2, ...):
31+
pass
32+
33+
func = decoarg(arg_a, arg_b)(func)
34+
35+
# is equivalent to the following:
36+
37+
@decoarg(arg_a, arg_b)
38+
def func(arg1, arg2, ...):
39+
pass

0 commit comments

Comments
 (0)