0

I've just started learning python from a video course and in this course as an example of polymorphism this code was provided:

class network:

    def cable(self): print('I am the cable')
    def router(self): print('I am the router')
    def switch(self): print('I am the switch')

class tokenRing(network):

    def cable(self): print('I am a token ring network cable')
    def router(self): print('I am a token ring router')

class ethernet(network):

    def cable(self): print('I am an ethernet network cable')
    def router(self): print('I am an ethernet router')

def main():

    windows=tokenRing()
    mac=ethernet()

    windows.cable()
    mac.cable()

main()

Now, I don't really think that's a good polymorphism example. I'm not sure that's a polymorphism at all! In fact, if I remove the network class (and, of course, the inheritance) the code works exactly the same. So I'm thinking that if you don't use the base class then that's not really polymorphism.

Am I right or wrong? Could someone modify this example so it actually presents the gist of polymorphism (i.e. actually makes use of the base class)?

NPS
  • 6,003
  • 11
  • 53
  • 90
  • 1
    It's not a good example of *Python* (recommended reading: http://www.python.org/dev/peps/pep-0008/), let alone polymorphism. Why not use an existing example (e.g. http://stackoverflow.com/q/3724110/3001761)? – jonrsharpe Oct 08 '15 at 15:24

2 Answers2

1

Polymorphism means changing the implementation of a base class's function in a subclass, so this example does demonstrate it. If you deleted the base class network then mac would no longer be able to call switch, so there is still some functionality left from the base class.

Of course it would be a better example if:

  • They called switch on mac in the main function, to demonstrate that the network class has been polymorphed, rather than just reimplemented.
  • They capitalised the class names, which is a Python convention that everybody uses.
benwad
  • 6,414
  • 10
  • 59
  • 93
0

The main point of the polymorphism is to provide an interface to different entities. In object-oriented programming, polymorphism is often achieved through subtyping polymorphism.

In your example you have Network, which is a kind of interface, with a bunch of entities, TokenRing and Ethernet.

In a language like Java or C++, where variables are typed, a common use of polymorphism is to create an array of interface and fill it with heterogeneous implementation. Then, you can use the methods declared by the interface on the concrete implementation.

Consider for example:

interface Animal {
  String talk();
}

class Cat implements Animal {
  String talk() {
    return "Meow";
  }
}

class Human implements Animal {
  String talk() {
    return "Hello";
  }
}

In your code, you'll now be able to make an array of animals, and blindly fill it with cat and human. Moreover, you can call talk() on each elements!

In Python, variables aren't typed, so advantages of polymorphism are harder to expose.
To conclude, if you are searching for an interface mechanism, you can take a look at Python's metaclass and the abc module.

Community
  • 1
  • 1
NiziL
  • 5,068
  • 23
  • 33
  • "In Python, variables aren't typed, so advantages of polymorphism are harder to expose." - **That's the whole point of my question!** I know the example code might technically use polymorphism but it doesn't expose it which a **good teaching example should**. – NPS Oct 08 '15 at 17:01
  • @NPS Yea, but a teaching example involving metaclass or the abc module should be even harder to understand for beginners. Actually, your code uses polymorphism, but I think (and it's really personnal) Python isn't the best language to introduce it :) – NiziL Oct 09 '15 at 08:17