[sf-lug] Python class: function passing vs calling

Alex Kleider a_kleider at yahoo.com
Thu Oct 9 08:10:56 PDT 2008

here's the little thing I put together Monday to try to illustrate the
difference between passing a function (no parentheses) and calling it.
By passing it is meant giving it to a function as an argument which then
becomes a parameter with in the function.

The next set of exercises(Chpt 4) demand some knowledge of trigonometry!
Developing the algorithms I found to be 'non-trivial!'

because it's not possible to send attachments to the list, it follows as
in line text:

# ThinkPython         attempt to show difference between 
#               use of a function name with or without parentheses

def first ():
  """ this function does something but ALSO returns a number
  print "first" 
  return 1

def second ():
  """ this function is a trivial alternative to the previous one
  print 'second'
  return 2

def use_function (f):
  """ this function simply calls a function passed to it as a parameter
        it's purpose is to demonstrate passing a function 
        as opposed to passing a function's return value

def number (n):
  """ this function assumes its argument will be printable
        and will print it
  print n

number (first())        # the argument is the value returned by first
number (second())       # ditto
use_function (first)    # the argument is the function,
                        # NOT its return value
use_function (second)   # ditto

# one can also assign a function to a new identifier

new_name = use_function

new_name (second)



More information about the sf-lug mailing list