Add some example docstrings for classes
This commit is contained in:
parent
215565451f
commit
4c91ef07fc
|
@ -19,6 +19,13 @@ from dataclasses import dataclass
|
|||
# before the class definition statement
|
||||
@dataclass
|
||||
class Shape():
|
||||
"""
|
||||
A class representing a generic shape.
|
||||
|
||||
Attributes:
|
||||
label (str): A text label for this shape
|
||||
fill (bool): Whether the shape is filled or outline
|
||||
"""
|
||||
label: str=""
|
||||
fill: bool = True
|
||||
|
||||
|
@ -27,6 +34,16 @@ class Shape():
|
|||
# in more detail in a subsequent tutorial.
|
||||
@dataclass
|
||||
class Circle(Shape):
|
||||
"""
|
||||
A class representing a circle shape.
|
||||
|
||||
Attributes:
|
||||
label (str) [parent]: A text label for this shape
|
||||
fill (bool) [parent]: Whether the shape is filled or outline
|
||||
center_x (int): The x coordinate for the cirlce center
|
||||
center_y (int): The y coordinate for the cirlce center
|
||||
radius (int): The radius of the circle
|
||||
"""
|
||||
center_x: int = 0
|
||||
center_y: int = 0
|
||||
radius: int = 0
|
||||
|
|
|
@ -8,6 +8,22 @@
|
|||
|
||||
# A class that describes a Person
|
||||
class Person():
|
||||
"""
|
||||
A class representing a person.
|
||||
|
||||
Class Attributes:
|
||||
species (str): The species of this person
|
||||
|
||||
Attributes:
|
||||
first_name (str): The person's first name
|
||||
last_name (str): The person's last name
|
||||
height (float): The person's height in feet
|
||||
weight (float): The person's weight in pounds
|
||||
|
||||
Methods:
|
||||
full_name(): Combines the first and last names
|
||||
__repr__: Provides a string representation of this class
|
||||
"""
|
||||
# This is a class variable. It can be accessed both from the base class
|
||||
# i.e. Person.species
|
||||
# Or from within an instance object
|
||||
|
@ -16,7 +32,16 @@ class Person():
|
|||
|
||||
# This is the method that gets called automatically when an instance of this class (an object) is created
|
||||
# Named parameters here passed into the object creation are declared with default values
|
||||
def __init__(self, first_name: int = "", last_name: int = "", height: float = 0.0, weight: float = 0.0):
|
||||
def __init__(self, first_name: str = "", last_name: str = "", height: float = 0.0, weight: float = 0.0):
|
||||
"""
|
||||
Constructs all the necessary attributes for this class.
|
||||
|
||||
Parameters:
|
||||
first_name (str): The person's first name
|
||||
last_name (str): The person's last name
|
||||
height (float): The person's height in feet
|
||||
weight (float): The person's weight in pounds
|
||||
"""
|
||||
# We must create instance variables from the passed parameters
|
||||
# Instance variables are only available within objects of a particular class type
|
||||
self.first_name = first_name
|
||||
|
@ -26,14 +51,37 @@ class Person():
|
|||
|
||||
# This is a simple method to return a full name as a concatination of first and last names
|
||||
def full_name(self):
|
||||
"""
|
||||
Concatenates the first and last names.
|
||||
|
||||
Returns:
|
||||
str of full name (first + last)
|
||||
"""
|
||||
return self.first_name + " " + self.last_name
|
||||
|
||||
# __repr__ is a special function called when you (for example) print() an object of this class type
|
||||
# It is meant to be used to generate a human readable and machine parseable representation of this class
|
||||
def __repr__(self):
|
||||
"""
|
||||
Creates a string representation of this class
|
||||
|
||||
Returns:
|
||||
str of the class name with all the named attributes and their values
|
||||
"""
|
||||
return f"{self.__class__.__name__}(species='{self.species}', first_name='{self.first_name}', last_name='{self.last_name}', height={self.height}, weight={self.weight})"
|
||||
|
||||
class Student(Person):
|
||||
"""
|
||||
A class representing a student. Inherits from the Person() class.
|
||||
|
||||
Attributes:
|
||||
first_name (str) [parent]: The person's first name
|
||||
last_name (str) [parent]: The person's last name
|
||||
height (float) [parent]: The person's height in feet
|
||||
weight (float) [parent]: The person's weight in pounds
|
||||
school_name (str): The name of the school the student attends
|
||||
id (int): The identification number of the student
|
||||
"""
|
||||
def __init__(self, school_name: str = "", id: int = 0):
|
||||
# We first call the parent's __init__ method to include it's attributes and methods here
|
||||
super().__init__()
|
||||
|
@ -44,6 +92,18 @@ class Student(Person):
|
|||
return f"{self.__class__.__name__}(species='{self.species}', first_name='{self.first_name}', last_name='{self.last_name}', height={self.height}, weight={self.weight}, school_name='{self.school_name}', id={self.id})"
|
||||
|
||||
class Employee(Person):
|
||||
"""
|
||||
A class representing an employee. Inherits from the Person() class.
|
||||
|
||||
Attributes:
|
||||
first_name (str) [parent]: The person's first name
|
||||
last_name (str) [parent]: The person's last name
|
||||
height (float) [parent]: The person's height in feet
|
||||
weight (float) [parent]: The person's weight in pounds
|
||||
company_name (str): The name of the company the employee works for
|
||||
id (int): The identification number of the employee
|
||||
salary (int): The salary of the employee in US dollars
|
||||
"""
|
||||
def __init__(self, company_name: str = "", id: int = 0, salary: int = 0):
|
||||
super().__init__()
|
||||
self.company_name = company_name
|
||||
|
|
Loading…
Reference in New Issue
Block a user