How to assign a function to another function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefano Esposito

    How to assign a function to another function

    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
    >>>def assigner (a, b):
    .... a = b
    ....
    >>>assigner(foo , bar)
    >>>foo()
    isn't working. How can I achieve my goal?

    Thanks for your help! :)
    --
    Stefano Esposito <stefano.esposi to87@gmail.com>
  • Paul Rudin

    #2
    Re: How to assign a function to another function

    Stefano Esposito <stefano.esposi to87@gmail.comw rites:
    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.

    Comment

    • Stefano Esposito

      #3
      Re: How to assign a function to another function

      On Mon, 17 Sep 2007 17:49:58 +0100
      Paul Rudin <paul.nospam@ru din.co.ukwrote:
      Stefano Esposito <stefano.esposi to87@gmail.comw rites:
      >
      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.esposi to87@gmail.com>

      Comment

      • Paddy

        #4
        Re: How to assign a function to another function

        On Sep 17, 6:11 pm, Stefano Esposito <stefano.esposi t...@gmail.com>
        wrote:
        On Mon, 17 Sep 2007 17:49:58 +0100
        >
        >
        >
        Paul Rudin <paul.nos...@ru din.co.ukwrote:
        Stefano Esposito <stefano.esposi t...@gmail.comw rites:
        >
        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.esposi t...@gmail.com>
        I got this explained to me awhile back and blogged on it here:
        Explaining why this works: n = [0] def list_access (): n[0] = n[0] + 1 return n try : print " \n list_access: ", list_access() e...


        - Paddy.

        Comment

        Working...