[Tutor] Python vs. Ruby
Alan Gauld
alan.gauld at blueyonder.co.uk
Fri Oct 31 04:12:36 EST 2003
> > I've used OO long enough to know there are times
> > when it ain't the best answer...
>
> When not?
> Isn't everything in Python an object, to some degree?
Yes, but Python doesn't insist that you use an OO approach
to the solution - as Ruby and Java do. You can write short
non OO programs in Ruby because there is an outer implies
class but once you get to bigger programs it becomes
difficult to create non OO modules, It's not impossible
but you have to work harder than in Python.
Many problems are better solved using a functional
programming approach. Others are more amenable to a
traditional imperative approach(ie procedural).
This is very true of batch programming where you traverse
a sequence of data applying some algorithm to each
item in the file. You can do it with objects but
usually there is little advantage.
Similarly very short programs rarely need the overhead of
classes - it just increases the code count for little gain.
Think of code libraries - look at Pythons as an example
- very few of the utility type libraries use an OO design.
Think of the Unix command line programs, most of them can
be written far more easily using simple functions than
by using objects.
[ Also the C++ standard library(the STL) is quite deliberately
non OO, it uses a functional programming approach. That
was done deliberately to make it as widely applicable as
possible. ]
OO really comes into its own where the problem gets bigger,
there are several instances of the same entities in memory
at one time, the entities have a lot of similarity etc.
So OO is very powerful and appropriate in many (probably most)
situations, but it is not the only solution. "When all you
have is a hammer, every problem looks like a nail" applies
in computing just as much as anywhere else!
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list