ahmet / Python-for-Data-Science
Herkese Açık Ahmet Kerem Piscine Final
S1E9.py 1.3 KB
from abc import ABC, abstractmethod
class Character(ABC):
"""Abstract representation of a character of the show."""
@abstractmethod
def __init__(self, first_name: str, is_alive: bool = True) -> None:
"""Build a character from a first name and a health state."""
self.first_name = first_name
self.is_alive = is_alive
def die(self) -> None:
"""Kill the character by turning is_alive to False."""
self.is_alive = False
class Stark(Character):
"""Representing the Stark family."""
def __init__(self, first_name: str, is_alive: bool = True) -> None:
"""Build a Stark from a first name and a health state."""
super().__init__(first_name, is_alive)
def main() -> None:
"""Test the Character abstraction and the Stark family."""
Ned = Stark("Ned")
print(Ned.__dict__)
print(Ned.is_alive)
Ned.die()
print(Ned.is_alive)
print(Ned.__doc__)
print(Ned.__init__.__doc__)
print(Ned.die.__doc__)
print("---")
Lyanna = Stark("Lyanna", False)
print(Lyanna.__dict__)
print("---")
try:
hodor = Character("Hodor")
print(f"Error: Character must stay abstract, got {hodor}")
except TypeError as error:
print(f"TypeError: {error}")
if __name__ == "__main__":
main()