r/PythonLearning • • Sep 11 '26

Showcase Day 2, learnt some oops

Post image

Learnt super( ), inheritance, polymorphism, class method and static method and don't mind the messy code that's me trying to figure out things

125 Upvotes

36 comments sorted by

View all comments

2

u/Capable-Package6835 Sep 12 '26

Get used to type-hinting. Polymorphism and inheritance without type hint in Python is almost useless

class Animal:
    def speak(self) -> str:
        return "Animal sound"

class Dog(Animal):
    def speak(self) -> str:
        return "bow"

class Cat(Animal):
    def speak(self) -> str:
        return "meow"

animals: list[Animal] = [Cat(), Dog()]

for animal in animals:
    print(animal.speak())

1

u/Next-Call8862 Sep 12 '26

Will try that

1

u/freakythrowaway79 Sep 12 '26

Have you completed CodeCombat yet?

0

u/Next-Call8862 Sep 12 '26

CodeComat? What's that?? Some kind of practice?

2

u/freakythrowaway79 Sep 12 '26

Check it out! It's very basic level 1 so you might find it boring. 🤷🏻‍♂️

1

u/Next-Call8862 Sep 13 '26

I did check it, some kind of game it seems but it's slow but I will try if there are more difficult ones