In this project, I learned about Python class inheritance. I learned about the differences between super- and sub-classes while practicing inheritance, definining classes with multiple base classes, and overiding inherited methods and attributes.
- tests: Folder of test files:
Prototypes for functions written in this project:
File | Prototype |
---|---|
0-lookup.py |
def lookup(obj): |
2-is_same_class.py |
def is_same_class(obj, a_class): |
3-is_kind_of_class.py |
def is_kind_of_class(obj, a_class): |
4-inherits_from.py |
def inherits_from(obj, a_class): |
101-add_attribute.py |
def add_attribute(obj, att, value): |
-
0. Lookup
- 0-lookup.py: Python function that returns a list of available attributes and methods of an objects.
-
1. My list
- 1-my_list.py: Python class
MyList
that inherits fromlist
. Includes:- Public instance method
def print_sorted(self):
that prints the list in ascending sorted order (assumes all list elements areint
s).
- Public instance method
- 1-my_list.py: Python class
-
2. Exact same object
- 2-is_same_class.py: Python function that returns
True
if an object is exactly an instance of a specified class; otherwiseFalse
.
- 2-is_same_class.py: Python function that returns
-
3. Same class or inherit from
- 3-is_kind_of_class.py: Python function that returns
True
if an object is an instance or inherited instance of a specified class; otherwiseFalse
.
- 3-is_kind_of_class.py: Python function that returns
-
4. Only sub class of
- 4-inherits_from.py: Python function that returns
True
if an object is an inherited instance (either directly or indirectly) from a specified class; otherwiseFalse
.
- 4-inherits_from.py: Python function that returns
-
5. Geometry module
- 5-base_geometry.py: An empty Python class
BaseGeometry
.
- 5-base_geometry.py: An empty Python class
-
6. Improve Geometry
- 6-base_geometry.py: Python class
BaseGeometry
. Builds on 5-base_geometry.py with:- Public instance method
def area(self):
that raises anException
with the messagearea() is not implemented
.
- Public instance method
- 6-base_geometry.py: Python class
-
7. Integer validator
- 7-base_geometry.py: Python class
BaseGeometry
. Builds on 6-base_geometry.py with:- Public instance method
def integer_validator(self, name, value):
that validates the parametervalue
. - Assumes the parameter
name
is always a string. - The parameter
value
must be anint
, otherwise, aTypeError
exception is raised with the message<name> must be an integer
. - The parameter
value
must be greater than0
, otherwise, aValueError
exception is raised with the message<value> must be greater than 0
.
- Public instance method
- 7-base_geometry.py: Python class
-
8. Rectangle
- 8-rectangle.py: Python class
Rectangle
that inherits fromBaseGeometry
(7-base_geometry.py). Includes:- Private attributes
width
andheight
- validated withinteger_validator
. - Instantiation with
width
andheight
:def __init__(self, width, height):
- Private attributes
- 8-rectangle.py: Python class
-
9. Full rectangle
- 9-rectangle.py: Python class
Rectangle
that inherits fromBaseGeometry
(7-base_geometry.py). Builds on 8-rectangle.py with:- Implementation of the method
area()
. - Special method
__str__
to printRectangle
s in the format[Rectangle] <width>/<height>
.
- Implementation of the method
- 9-rectangle.py: Python class
-
10. Square #1
- 10-square.py: Python class
Square
that inherits fromRectangle
(9-rectangle.py). Includes:- Private attribute
size
- validated withinteger_validator
. - Instantiation with
size
:def __init__(self, size):
. - Implementation of the
area()
method.
- Private attribute
- 10-square.py: Python class
-
11. Square #2
- 11-square.py: Python class
Square
that inherits fromRectangle
(9-rectangle.py). Builds on 10-square.py with:- Special method
__str__
to print squares in the format[Square] <width>/<height>
.
- Special method
- 11-square.py: Python class
-
12. My integer
- 100-my_int.py: Python class
MyInt
that inherits fromint
. Includes:- Inversion of the
==
and!=
operators.
- Inversion of the
- 100-my_int.py: Python class
-
13. Can I?
- 101-add_attribute.py: Python function that adds a new attribute to an object if possible.
- If an attribute cannot be added, a
TypeError
exception is raised with the messagecan't add new attribute
.
- If an attribute cannot be added, a
- 101-add_attribute.py: Python function that adds a new attribute to an object if possible.