I meant to add...

On 04/01/14 20:47, Alan Gauld wrote:

Its called polymorphism and is one of the most powerful advantages of
OOP since case or switch statements are one of the most fault prone
structures in procedural programming.
...
Without OOP you would need to do something like

for shape in shapes:
     if shape['type'] == CIRCLE:
         result = circle_area(shape['radius'])
     elif shape['type'] == SQUARE:
         result = square_area(shape['length'])
     elif ....

But with OOP we simply call each shapes area method and
the interpreter works out which method to call:

for shape in shapes:
  result = shape.area()

This reduction in code in switch statements (and similar savings in other scenarios) is one of the reasons that OOP solutions are often much shorter than non OOP programs for non-trivial cases.

Beginners often don't see these benefits because their short programs only have one or two classes and the overhead of creating the classes dwarfs the savings that might accrue. But in a big project where lots of if/else type situations may arise the code savings can easily add up to 20-30%.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to