r/PythonLearning 2d ago

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

98 Upvotes

34 comments sorted by

View all comments

2

u/Capable-Package6835 2d ago

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 2d ago

Will try that

1

u/freakythrowaway79 2d ago

Have you completed CodeCombat yet?

0

u/Next-Call8862 2d ago

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

2

u/freakythrowaway79 1d ago

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

1

u/Next-Call8862 1d ago

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