In this project, I began practicing object-oriented programming using classes and objects in Python. I learned about attributes, methods, and properties as well as data abstraction, data encapsulation, and information hiding.
-
0. My first square
- 0-square.py: Python class
Squarethat defines a square.
- 0-square.py: Python class
-
1. Square with size
- 1-square.py: Python class
Squarethat defines a square. Builds on 0-square.py with:- Private instance attribute
size. - Instantiation with
size.
- Private instance attribute
- 1-square.py: Python class
-
2. Size validation
- 2-square.py: Python class
Squarethat defines a square. Builds on 1-square.py with:- Instantiation with optional
size:def __init__(self, size=0):
- Instantiation with optional
- If a provided
sizeattribute is not an integer, aTypeErrorexception is raised with the messagemust be an integer. - If a provided
sizeattribute is less than0, aValueErrorexception is raised with the messagesize must be >= 0.
- 2-square.py: Python class
-
3. Area of a square
- 3-square.py: Python class
Squarethat defines a square. Builds on 2-square.py with:- Public instance attribute
def area(self):that returns the current square area.
- Public instance attribute
- 3-square.py: Python class
-
4. Access and update private attribute
- 4-square.py: Python class
Squarethat defines a square. Builds on 3-square.py with:- Property
def size(self):to retrieve the private instance attributeself. - Property setter
def size(self, value):to setself.
- Property
- 4-square.py: Python class
-
5. Printing a square
- 5-square.py: Python class
Squarethat defines a square. Builds on 4-square.py with:- Public instance method
def my_print(self):that prints the square with the character#to standard output (ifsize== 0 -> prints an empty line).
- Public instance method
- 5-square.py: Python class
-
6. Coordinates of a square
- 6-square.py: Python class
Squarethat defines a square. Builds on 5-square.py with:- Private instance attribute
position. - Property
def position(self):to retreiveposition. - Property setter
def position(self, value):to setposition. - Instantiation with optional
sizeandposition:def __init__(self, size=0, position=(0, 0)):
- Private instance attribute
- If a provided
positionattribute is not a tuple of two integers, aTypeErrorexception is raised with the messageposition must be a tuple of 2 positive integers.
- 6-square.py: Python class
