what is a function object?

claudius at catlover.com claudius at catlover.com
Tue Nov 9 16:18:28 EST 1999


zakons at my-deja.com says:
>Hi,
>I use function objects for sorting lists and they are great but I want
>to know more about what a function object is in the python environment.

I'll take a stab at answering this...

>* How is a function object different from other python objects? What
>does it include extra? What is missing?

>From what I gather, the most important difference is the presence of a __call__
method.

>* How may function objects be accessed by name in various namespaces?

Function objects can be referenced by name without specifying a parameter list.
I.e.

def foo(bar):
	print 'baz %s' % bar
alpha = foo
alpha('hello world') # will print out 'baz hello world'

>* Other than for sorting, what are typical uses of function objects?

Creating callbacks, storing regular functions in other datastructures?
(Excellent for writing parsers.)

MOOP (http://www.accessoft.com/moop) creates function objects that are methods
for the MOOP environment.  (And that are pickle-friendly.)  Another of my
projects (work projects, hence I can't share the source) does a similiar
thing, creating 'methods' that reside on instances (normally methods are
attributes of classes.)

>* What does it mean to say that function objects are first-class objects
>in the python environment?

Good question...I have no idea.




More information about the Python-list mailing list