Python Forum
Polymorphism in Python Question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Polymorphism in Python Question
#1
Coming from Java, I'm wondering how in Python you "program to interface, not to implementation", when Python doesn't have, well, interfaces?

For example, in Java, you could create a class hierarchy like Animal->Dog. Then, in code, you could create an object like this (pseud ocode):

Class Animal

Class Dog extends Animal
.
.
.
Animal animal = new Dog(); // polymorphism in Java
animal.makeSound();

Here, the variable dog has an Animal "type", but is an instance of a Dog class.

---

In Python, can you do the same type of thing?

class Animal():
def sound(self):
print("Basic Animal")

class Dog(Animal):
def sound(self):
print("Woof woof!")

===

How would you complete this code so it's polymorphic (using the Animal class to reference the dog class as in the Java example above)?

Thanks,
Reply
#2
You can define Animal to be an abstract base class: https://docs.python.org/3/library/abc.html or just do what you're doing here.

class Animal():
    def sound(self):
       print("Basic Animal")

class Dog(Animal):
    def sound(self):
        print("Woof woof!")

class Cat(Animal):
    pass  

menagerie = [Dog(), Cat()]
for creature in menagerie:
    creature.sound()
Reply
#3
There is no concept on interface in python. However you can define an abstract class. And you do this by inheriting from class called ABC. See the link bellow to know more about ABC.

https://docs.python.org/2/library/abc.html

class Animal(ABC):

    def __init__(self):
        super.__init__()

    @abstractmethod
    def sound(self):
        pass


class Dog(Animal):

    def __init__(self):
        pass

    def sound(self):
        return "Im dog"
Reply
#4
(Dec-13-2017, 02:37 PM)hshivaraj Wrote: There is no concept on interface in python. However you can define an abstract class. And you do this by inheriting from class called ABC. See the link bellow to know more about ABC.

https://docs.python.org/2/library/abc.html

class Animal(ABC):

    def __init__(self):
        super.__init__()

    @abstractmethod
    def sound(self):
        pass


class Dog(Animal):

    def __init__(self):
        pass

    def sound(self):
        return "Im dog"

But, it seems to use this in Python, you have to program to the class implementation you want to use, not a more generic abstract class.

IOW, from your example, I don't see how to have a variable based on Animal that is an instance of Dog.
Reply
#5
Quote:IOW, from your example, I don't see how to have a variable based on Animal that is an instance of Dog.
Python is dynamically typed language and therefore the concept of dynamic binding is not relevant. The point of using an abstract class is to enforce the subclass to implement certain methods in the deriving class. And also you cant instigate an object of type class which has an abstract method.
Reply
#6
I must be doing it wrong then because i just leave it as pass to notify that it is not used...and maybe put in a doc string
class Animal:
    def sound(self):
        pass 
        
class Dog(Animal):
    def sound(self):
        print('woof')
And if i really did need to notify i would use the NotImplemented error if it wasnt a crazy amount of methods.
class Animal:
    def sound(self):
        raise NotImplemented('What are you doing?')
Recommended Tutorials:
Reply
#7
(Dec-13-2017, 04:06 PM)hshivaraj Wrote:
Quote:IOW, from your example, I don't see how to have a variable based on Animal that is an instance of Dog.
Python is dynamically typed language and therefore the concept of dynamic binding is not relevant. The point of using an abstract class is to enforce the subclass to implement certain methods in the deriving class. And also you cant instigate an object of type class which has an abstract method.

Gotcha. This is one area where Python is much different (not in a bad way) than Java.

Appreciate your reply.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Polymorphism not working with a call to a abstract method colt 3 2,275 Nov-04-2019, 11:04 PM
Last Post: colt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020