Function reference -> name?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rene Pijlman

    Function reference -> name?

    When I have a reference to a function (as in parameter 'function' of f1
    below), how can I get the name of that function ('f2' in the example
    below)?

    def f1(function):
    pass
    # How to get 'f2' from function?

    def f2():
    pass

    f1(f2)

    --
    René Pijlman
  • Emile van Sebille

    #2
    Re: Function reference -> name?


    "Rene Pijlman" <reply.in.the.n ewsgroup@my.add ress.is.invalid > wrote in
    message news:2lo9rvsob1 ejjju20ut308tem f0p8l0gbo@4ax.c om...[color=blue]
    > When I have a reference to a function (as in parameter 'function' of[/color]
    f1[color=blue]
    > below), how can I get the name of that function ('f2' in the example
    > below)?
    >
    > def f1(function):
    > pass
    > # How to get 'f2' from function?
    >
    > def f2():
    > pass
    >
    > f1(f2)
    >[/color]
    [color=blue][color=green][color=darkred]
    >>> def f1():pass[/color][/color][/color]
    ....[color=blue][color=green][color=darkred]
    >>> f1.func_name[/color][/color][/color]
    'f1'[color=blue][color=green][color=darkred]
    >>>>>> dir (f1)[/color][/color][/color]
    ['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
    '__get__', '__ge
    tattribute__', '__hash__', '__init__', '__module__', '__name__',
    '__new__', '__r
    educe__', '__reduce_ex__' , '__repr__', '__setattr__', '__str__',
    'func_closure',
    'func_code', 'func_defaults' , 'func_dict', 'func_doc',
    'func_globals', 'func_na
    me']




    HTH,

    --

    Emile van Sebille
    emile@fenx.com


    Comment

    • Duncan Booth

      #3
      Re: Function reference -&gt; name?

      Rene Pijlman <reply.in.the.n ewsgroup@my.add ress.is.invalid > wrote in
      news:2lo9rvsob1 ejjju20ut308tem f0p8l0gbo@4ax.c om:
      [color=blue]
      > When I have a reference to a function (as in parameter 'function' of f1
      > below), how can I get the name of that function ('f2' in the example
      > below)?
      >
      > def f1(function):
      > pass
      > # How to get 'f2' from function?[/color]
      print function.__name __[color=blue]
      >
      > def f2():
      > pass
      >
      > f1(f2)
      >[/color]
      This will give you the name under which the function was originally
      defined, which of course may not be the name of the variable at the time
      you passed it in. Also if it was a lambda function you get the name
      '<lambda>', and if it is some other sort of callable object it may or may
      not have a __name__ attribute.

      --
      Duncan Booth duncan@rcp.co.u k
      int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
      "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

      Comment

      • Rene Pijlman

        #4
        Re: Function reference -&gt; name?

        Emile van Sebille:[color=blue]
        >Rene Pijlman:[color=green]
        >> When I have a reference to a function, how can I get the name of that function?[/color][/color]
        [...][color=blue][color=green][color=darkred]
        >>>>>>> dir (f1)[/color][/color][/color]

        Of course.

        I forgot to mention that the function reference is an XML-RPC client stub,
        which makes quite a difference :-)

        dir() told me I need functionReferen ce._Method__nam e in that case.

        Thanks Emile and Duncan.

        --
        René Pijlman

        Comment

        • Michel Claveau/Hamster

          #5
          Re: Function reference -&gt; name?

          Apply ?



          Comment

          Working...