How to assign a function to another function

Paddy paddy3118 at googlemail.com
Mon Sep 17 17:21:27 EDT 2007


On Sep 17, 6:11 pm, Stefano Esposito <stefano.esposit... at gmail.com>
wrote:
> On Mon, 17 Sep 2007 17:49:58 +0100
>
>
>
> Paul Rudin <paul.nos... at rudin.co.uk> wrote:
> > Stefano Esposito <stefano.esposit... 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.
>
> This worked, thanks a lot :)
>
> --
> Stefano Esposito <stefano.esposit... at gmail.com>

I got this explained to me awhile back and blogged on it here:
  http://paddy3118.blogspot.com/2006/07/python-functions-assignments-and-scope.html

- Paddy.




More information about the Python-list mailing list