[Tutor] Q: Do Functions Have Any Class?

Benoit Dupire bdupire@seatech.fau.edu
Wed, 28 Mar 2001 18:17:22 -0500


D-Man wrote:

> On Tue, Mar 27, 2001 at 11:27:31AM -0600, Curtis Larsen wrote:
> | Besides their design and concept differences, is there really any
> | (performance-based) reason to use classes/methods in a script as opposed
> | to using functions?  Does it depend on how large your main script is?
>
> If you have a simple script, or some operation that doesn't have any
> persistant state (such as math operations, for example) it is better
> to use a function.
>
> Take Java for example.  Java forces one to use a class for everything.
> As a result, the Math class is composed entirely of static methods
> becuase things like sqrt and pow don't have any state.  It is a
> ridiculous design brought about by the requirement of classes.

I don't think that Java is the first OO language to come up with such a
design...

It just makes it coherent with the rest of  the language: these functions
don't need a state so let's use them without defining a new object.
But let's regroup them in the same class, because they really do belong to
this class.... why ?
Because a class is really something which is conceptual: it represents
something coherent.
The sqrt function is a behavior of the math concept..

In Python, there is another way to implement it:  you've got a math module and
some functions in it.
But a Python module can also have a state (its global variables)
What is the difference between a Python module and a class, when you don't
instanciate the latter one ?
Well you can derive the class and extend it....
I think that's coherent, if you want to add your own maths functions...
In the ""module"" way to do it, you have to open the math module which comes
with the Python distribution, edit it, and save it ?
Is this better? It's really up to you. I never had to add another math
function. But for other examples (parser...) it might be useful to have a
class,  (override a method, so that it behaves differently for the same
token). For the Math module, you can also override a method, for example if
you want sqrt() to return you something specific..

At the starting point, the OO 'spirit' consists of  gaining benefits from
modularity.. we want to build reusable module (not in the Python sense)
A Module is just a reasonable independant chunk of a program, because there is
a limit to how much one person can understand at any one time.
A system is a collection of modules.... Modules might be: files, functions,
procedures, subroutines, classes, larger language constructs (called modules
in Python), or independant programs or subsystems.
It depends on the context. Much of what makes a good module is true of modules
at many levels of abstraction...
So coming up with a Math class is not so weird.

If you would like your language to be pure OO, then everything is an object,
even integer and floats...
so
a = Float();
a.sqrt()
where sqrt is a method, will be the correct way to do it.

I heard that in Ruby, all was object... I don't know if they implement the
language in this way ( a.sqrt() ) , but i assume yes...

This brings us  to the performance issue...
I read in the Python newsgroup that Ruby was slower than Python for programs
dealing with a lot of computations...
Why ? because every thing is object, and OO is no faster than the structural
programming paradigm (the extra-layer to access the data...)
So, at this point, you begin to worry about it.



Benoit