Jeff Kyei
Jeff Kyei

Reputation: 33

Polymorphism in Python via abstraction

I'm trying to achieve polymorphism in python via abstraction. Here's my code:

class Animal:
    def talk(self):
        print('Hello')

class Dog(Animal):
    def talk(self):
        print('Bark')

class Cat(Animal):
    def talk(self):
        print('meow')

myObj = Dog()
print(myObj.talk())

this achieves the purpose except when I create an object and print, I get the following output:

Bark
None

I was expecting to get just Bark. Can anyone explain to me why None is also printed?

Upvotes: 0

Views: 54

Answers (1)

bb1
bb1

Reputation: 7863

None is the return value of the talk() method, since this method does not return any value explicitly. If you execute myObj.talk() (without print()) you will get Bark only.

Upvotes: 3

Related Questions