How to assign a function to another function

Paul Rudin paul.nospam at rudin.co.uk
Mon Sep 17 12:49:58 EDT 2007


Stefano Esposito <stefano.esposito87 at gmail.com> writes:

> Hi all
>
> what i'm trying to do is this:
>
>>>>def foo ():
> ...	return None
> ...
>>>>def bar ():
> ...	print "called bar"
> ...
>>>>def assigner ():
> ...	foo = bar
> ...
>>>>assigner()
>>>>foo()
> called bar
>>>>
>
> This piece of code is not working and even trying with...


> ... How can I achieve my goal?


By adding the line:
   global foo
at the beginning of the body of assigner.

The assignment to foo in the body of assigner makes a local (to the
function) variable called foo and assigns bar to it.




More information about the Python-list mailing list