Function pointers in Python?

Bjorn Pettersen bjorn at roguewave.com
Tue Sep 12 23:07:30 CEST 2000


def foo(x):
  print x

def bar(y):
  return y * y

d = {
 'fooval':foo,
 'barval':bar
}

apply(d['fooval'], ('hello world',) )
print apply(d['barval'], (666,) )

hth,
-- bjorn

Richard Harvey wrote:
> 
> I know there is no direct correlation to C function pointers in Python, but
> I'm curious if I can pull of a similar effect. I'm wanting to define a
> dictionary that pairs a value with a Python function. If I'm provided the
> value, I would find the matching dictionary entry and then call the Python
> function that was paired with the value.  If there is a way to do this, how
> would I assign the function name into the dictionary, and how should I
> retrieve the function and call it from the dictionary?
> 
> Rich
> 
> --
> http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list