Skip to content
This repository was archived by the owner on Aug 11, 2023. It is now read-only.

Commit fb752a8

Browse files
committed
Update
1 parent cffc32d commit fb752a8

8 files changed

+157
-4
lines changed

README-PYTHON-HOMEWORKS.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,13 @@
121121
- Date, Time, Format
122122
- Math
123123
- def, lambda
124+
- Review concepts
125+
126+
## TP09
127+
- [OOP](lessons/python/concepts/object-oriented/README.md)
128+
- [Decorator](lessons/python/concepts/advanced/simple-decorator.py)
129+
- [NumPy](lessons/python/modules/numpy/README.md)
124130
- argv, kwargs
125-
- Inheritance
126-
- Decorator
127-
- NumPy
128131
- ...
129132

130133

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
18. Math
110110
19. [Date, Time](/lessons/python/concepts/date-time/date-time.py)
111111
14. [Function, Method, Lambda](/lessons/python/concepts/object-oriented/types-of-methods.py)
112-
20. [OOP](/lessons/python/concepts/object-oriented/object-oriented.py)
112+
20. [OOP](/lessons/python/concepts/object-oriented/README.md)
113113
- Class, Object
114114
- Constructor
115115
- ToString, Representation
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# OOP
2+
3+
- Each object has a unique identity.
4+
- Each object can contain several other objects.
5+
- Each object can have a series of attributes.
6+
- Every object has a set of behaviors.
7+
8+
- An object is an instance created from a class. The process of making this sample is called instantiation.
9+
10+
## 4 fundamental concepts of object oriented programming
11+
- Abstraction
12+
- When we talk about "animal", we are not referring to a specific animal. This is an abstract concept.
13+
- Abstraction occurs when a programmer hides irrelevant data about an object or class to reduce complexity.
14+
- Polymorphism
15+
- Ability to take different forms.
16+
- [With inheritance and class method](polymorphism-with-inheritance.py)
17+
- [With function](polymorphism-with-functions.py)
18+
- [Inheritance](oop-inheritance.py)
19+
- Is a mechanism that allows one class to inherit the properties of another class, in the same way that a child inherits certain properties from each of its parents.
20+
- [Encapsulation](oop-encapsulation.py)
21+
- Means "putting one or more items in a physical or logical package." Encapsulation in object-oriented programming limits access to implementation details.
22+
- Is implemented by access specifiers. Access level attributes define the scope of class members.
23+
- [Access Modifiers](oop-access-modifiers.py)
24+
- public
25+
- protected
26+
- private
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class MyClass:
2+
name = "PUBLIC"
3+
_family = "PROTECTED"
4+
__middle = "PRIVATE"
5+
6+
def get_private_variable(self):
7+
return self.__middle
8+
9+
@property
10+
def family(self):
11+
return self._family
12+
13+
@family.setter
14+
def family(self, value):
15+
self._family = value
16+
17+
18+
# ------------------------------------------------------------
19+
# Public member
20+
21+
print(MyClass.name)
22+
23+
# ------------------------------------------------------------
24+
# Protected member - Property
25+
26+
print(MyClass._family) # Accessible - but do not use it like this
27+
28+
cls = MyClass()
29+
print(cls.family)
30+
cls.family += "-Changed"
31+
print(cls.family)
32+
print(cls._family) # Accessible - but do not use it like this
33+
34+
# ------------------------------------------------------------
35+
# Private member
36+
37+
# print(MyClass.__middle) # AttributeError: type object 'MyClass' has no attribute '__middle'
38+
print(MyClass().get_private_variable())
39+
40+
41+
# ------------------------------------------------------------
42+
class DerivedClass(MyClass):
43+
name="PUBLIC2"
44+
_family="PROTECTED2"
45+
__middle = "New Private Middle Name"
46+
47+
48+
cls = MyClass()
49+
dcls = DerivedClass()
50+
51+
print(cls.name," --- ", dcls.name)
52+
print(cls._family," --- ", dcls._family)
53+
print(cls.get_private_variable()," --- ", dcls.get_private_variable()) # __middle in new class is not shadowed the original one
54+
"""
55+
PUBLIC --- PUBLIC2
56+
PROTECTED --- PROTECTED2
57+
PRIVATE --- PRIVATE
58+
"""
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Counter:
2+
__counter = 0 # Private member
3+
4+
def inc(self):
5+
self.__counter += 1
6+
7+
def dec(self):
8+
self.__counter -= 1
9+
10+
def reset(self):
11+
self.__counter = 0
12+
13+
def __str__(self) -> str:
14+
return str(self.__counter)
15+
16+
17+
counter1 = Counter()
18+
print(counter1)
19+
counter1.inc()
20+
print(counter1)
21+
counter1.reset()
22+
print(counter1)
23+
24+
# print(counter1.__counter) #AttributeError: 'Counter' object has no attribute '__counter'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Parent:
2+
pass
3+
4+
class Child(Parent):
5+
pass
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def rectangle_area(x,y=None):
2+
if y is None:
3+
return x*x
4+
else:
5+
return x*y
6+
7+
print(rectangle_area(12))
8+
print(rectangle_area(12,13))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Shape:
2+
def area(self):
3+
return -1
4+
5+
def __init__(self, *values) -> None:
6+
self.value = values
7+
8+
9+
class Rectangle(Shape):
10+
def area(self):
11+
return self.value[0] * self.value[1]
12+
13+
14+
class Triangle(Shape):
15+
def area(self):
16+
return (self.value[0] * self.value[1]) / 2
17+
18+
19+
class Square(Shape):
20+
def area(self):
21+
return self.value[0] * self.value[0]
22+
23+
24+
s1 = Rectangle(10, 12)
25+
s2 = Triangle(10, 12)
26+
s3 = Square(10)
27+
28+
for shape in [s1, s2, s3]:
29+
print(shape.area())

0 commit comments

Comments
 (0)