Unification of Methods and Functions

Antoon Pardon apardon at forel.vub.ac.be
Tue May 25 07:31:20 EDT 2004


Op 2004-05-25, David MacQuigg schreef <dmq at gain.com>:
> On 25 May 2004 10:30:03 GMT, Antoon Pardon <apardon at forel.vub.ac.be>
> wrote:
>
>>Op 2004-05-25, David MacQuigg schreef <dmq at gain.com>:
>>> On 25 May 2004 07:39:49 GMT, Antoon Pardon <apardon at forel.vub.ac.be>
>>> wrote:
>>>>
>>>>Well one possible view is that methods are just functions. The
>>>>difference being that it is not exactly the function as it
>>>>is defined in the class but as related function. Now I don't
>>>>know if trying to explain methods from this point of view
>>>>would be easier for students to understand and it may cause
>>>>confusions of its own. But I think this approach deserves
>>>>more attention you seem willing to give it.
>>>
>>> I don't feel I can make much improvement on Learning Python, 2nd ed.
>>> I think this is the same approach that you are talking about -
>>> functions first, then methods, one variation at a time.
>>
>>I don't think so. The apprach I'm thinking of would
>>be about the following.
>>
>>1) functions
>>2) bare classes (no methods)
>>3) higher order functions
>>4) show how higher order functions
>>   can result in method like beheviour
>>5) methods and show the connection with
>>   higher order functions.
>
> I've started a section on my webpage at
> http://ece.arizona.edu/~edatools/Python/Examples 
> for those that feel we need a completely different approach to
> introducing OOP.  If you put together some examples of what you mean
> by the above, I'll add them to my webpage.

First of all. I don't know if a completly different approach
is usefull. I don't claim my approach will be helpfull for
anyone. I just hope this approach can clear things up for
people who are confused by other approaches.

Now I'm not going to explain functions and bare classes
since I don't think there are any problems with those
concepts.

3) Higher order functions. These are functions that
   whose arguments and/or return values can be again
   functions.

The typical example is a sorting function that takes
as an argument a function that decides which of two
elements is the smaller. By providing different
functions (one that looks at names or one that
looks at Id number) the same function can be used
to sort people alphabetical or Numerical by Id.

Now to go a step further. Suppose we have a lot
of functions Fi: Each of these functions is
defined as follows Fi(x) = i + x. Now we
can have an other function S. S is defined as
follows: S(a) = Fa. Now we can use S to add
two numbers. Suppose we want to add 2 and 5.
First we call S(2), this will result in F2.
Then we call F2(5) which will result in 7.
Or more directly we could call S(2)(5).

Now python allows to define higher order functions.
S would be defined as follows in python:

def S(i):

  def Fi(x):

    return i + x

  return Fi


The rest will be for future posts. If you have
questions or remarks don't hesitate.

-- 
Antoon Pardon



More information about the Python-list mailing list