0

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?

1
  • You should call the method but not print myObj.talk() Commented Mar 24, 2021 at 4:16

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.