From 4d13762c095d650687de64816c589be25b56daa0 Mon Sep 17 00:00:00 2001 From: Geo Halkiadakis Date: Fri, 14 Jul 2023 16:59:46 +0300 Subject: unrelated scripts (sorry) --- python/unrelated/obj.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 python/unrelated/obj.py (limited to 'python/unrelated') diff --git a/python/unrelated/obj.py b/python/unrelated/obj.py new file mode 100644 index 0000000..c20502f --- /dev/null +++ b/python/unrelated/obj.py @@ -0,0 +1,71 @@ +# Animal +# base class +class Animal: + + def __init__(self, name, age): + self.name = name + self.age = age + + def preview(self): + print(f"{self.name} is a {self.creature} of {self.age} years old") + + + +# Dog +# extends Animal +class Dog(Animal): + + voice = 'woof! ~@_@~' + + def __init__(self, name, age): + self.name = name + self.age = age + self.creature = 'dog' + + def speak(self): + print(self.voice) + + + +# Cat +# extends Animal +class Cat(Animal): + + voice = 'meao!! >^_^<' + + def __init__(self, name, age): + self.name = name + self.age = age + self.creature = 'cat' + + def speak(self): + print(self.voice) + + +# functions +# --- -- -- - - - + +def newLine(): + print(" ") + +def attrs(obj): + for attribute, value in obj.__dict__.items(): + print(f"{attribute}: {value}") + + +# instances +# --- -- -- - - - + +d = Dog('Ersi', 8) +r = Cat('Riri', 10) + +d.preview() +d.speak() +attrs(d) + +newLine() + +r.preview() +r.speak() +attrs(r) + -- cgit v1.2.3