Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
110 lines (72 sloc)
2.81 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Source Code Form is subject to the terms of the Mozilla Public | |
# License, v. 2.0. If a copy of the MPL was not distributed with this | |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
"""03 - Overriding class attributes in Python. | |
Use inheritance to override a class attribute of the Person class and call | |
method of base class. | |
""" | |
import typing | |
class Person: | |
"""A person has a name and likes to do things.""" | |
def __init__(self, name: str): | |
self.name = name | |
def __str__(self) -> str: | |
return f"{self.name}" | |
def stay_hydrated(self) -> None: | |
print(f"{self} drinks some water. π°") | |
def go_to_the_movies(self) -> None: | |
print(f"{self} goes to the movies. πΏ") | |
def go_hiking(self) -> None: | |
print(f"{self} goes hiking. β°") | |
def build_a_robot(self) -> None: | |
print(f"{self} builds a robot. π€") | |
class Project: | |
"""A project has a board_name and a description.""" | |
def __init__(self, board_name: str, description: str): | |
self.board_name = board_name | |
self.description = description | |
def __str__(self) -> str: | |
return f"Project '{self.board_name}'" | |
class TeamMember(Person): | |
"""A team member is a person, who works on projects, and may have | |
specialized in a specific field. | |
""" | |
expertise: typing.Optional[str] = None | |
def __str__(self) -> str: | |
# Get default string representation from the super class | |
default = super().__str__() | |
if self.expertise is None: | |
return f"{default}" | |
return f"{self.expertise} {default}" | |
def work_on_project(self, project: Project) -> None: | |
"""Start working on the given project.""" | |
print(f"{self} is now working on {project}. π") | |
self.stay_hydrated() | |
class MobileEngineer(TeamMember): | |
"""Team member specialized in developing for mobile platforms.""" | |
expertise = "π±" | |
class DataScientist(TeamMember): | |
"""Team member specialized in data science.""" | |
expertise = "π" | |
class ProjectManager(TeamMember): | |
"""Team member specialized in project management.""" | |
expertise = "π" | |
class OperationsEngineer(TeamMember): | |
"""Team member specialized in running cloud infrastructure.""" | |
expertise = "π¦" | |
if __name__ == "__main__": | |
simone = OperationsEngineer("Simone") | |
simone.go_to_the_movies() | |
simone.build_a_robot() | |
simone.go_hiking() | |
chelsea = DataScientist("Chelsea") | |
dave = ProjectManager("Dave") | |
marlene = MobileEngineer("Marlene") | |
data_platform = Project( | |
board_name="Data Platform", | |
description="Platform providing datasets and data viewing tools.", | |
) | |
simone.work_on_project(data_platform) | |
chelsea.work_on_project(data_platform) | |
dave.work_on_project(data_platform) | |
marlene.work_on_project(data_platform) |