Person and Dog with their own greeting methodsgreet that takes one argument and calls the correct greeting methodgreet function with a Person instance and a Dog instancePerson and Dog with their own greeting methodsgreet that takes one argument and calls the correct greeting methodgreet function with a Person instance and a Dog instancePerson with a method say_hello that returns the string "Hello, I am a person.". Also create a class called Dog with a method say_hello that returns the string "Woof! I am a dog.".Remember to use def say_hello(self): inside each class and return the exact greeting strings.
greet that takes one parameter called entity. Inside the function, call the say_hello method of entity and return its result.Use entity.say_hello() to call the method on the passed object.
person and assign it an instance of the Person class. Create another variable called dog and assign it an instance of the Dog class.Use person = Person() and dog = Dog() to create the objects.
print function to display the result of calling greet(person) and then greet(dog).Use print(greet(person)) and print(greet(dog)) to show the greetings.
What does polymorphism through functions mean in Python?
Which of the following is the correct way to check a variable's type inside a function for polymorphism?
def process(value):
# What to use here?
passWhat is the output of this code?
def describe(value):
if isinstance(value, int):
return f"Integer: {value}"
elif isinstance(value, str):
return f"String: {value}"
else:
return "Unknown type"
print(describe(10))
print(describe('hello'))
print(describe(3.14))Find the error in this polymorphic function and fix it:
def process(value):
if isinstance(value, int):
return value * 2
elif isinstance(value, str):
return value + value
else:
return value / 2
print(process('abc'))
print(process([1, 2, 3]))Write a polymorphic function combine that accepts either two strings or two lists and returns their concatenation. What is the output of this code?
def combine(a, b):
if isinstance(a, str) and isinstance(b, str):
return a + b
elif isinstance(a, list) and isinstance(b, list):
return a + b
else:
return None
print(combine('Hi, ', 'there!'))
print(combine([1, 2], [3, 4]))
print(combine('Hello', [1, 2]))Pass the test and this pattern moves unknown