Name of the function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Olaf Meding

    Name of the function

    I am looking for a way to get at the name of the function (while
    executing code inside the function) without (!) knowing the name of
    the function.

    The code below works, but requires the name of the function. I am
    looking for a way to do the same w/o specifying the name of the
    function.

    def olaf():
    # requires name of the function
    print olaf.__name__

    # this does not work
    print self.__name__
  • anton muhin

    #2
    Re: Name of the function

    Olaf Meding wrote:[color=blue]
    > I am looking for a way to get at the name of the function (while
    > executing code inside the function) without (!) knowing the name of
    > the function.
    >
    > The code below works, but requires the name of the function. I am
    > looking for a way to do the same w/o specifying the name of the
    > function.
    >
    > def olaf():
    > # requires name of the function
    > print olaf.__name__
    >
    > # this does not work
    > print self.__name__[/color]

    This seems to work:

    def foo():
    print inspect.current frame().f_code. co_name

    regards,
    anton.

    Comment

    • Olaf Meding

      #3
      Re: Name of the function

      Is there a way to do this w/o importing another module?

      Yes this is what I was looking for except the code below requires
      importing module inspect.
      [color=blue]
      > def foo():
      > print inspect.current frame().f_code. co_name[/color]

      Thanks much for your reply.


      Olaf

      Comment

      • Terry Reedy

        #4
        Re: Name of the function


        "anton muhin" <antonmuhin@ram bler.ru> wrote in message
        news:btjuct$84v 33$1@ID-217427.news.uni-berlin.de...[color=blue]
        > Olaf Meding wrote:[color=green]
        > > I am looking for a way to get at the name of the function (while
        > > executing code inside the function) without (!) knowing the name of
        > > the function.
        > >[/color]
        > This seems to work:
        >
        > def foo():
        > print inspect.current frame().f_code. co_name[/color]

        as does this ...
        [color=blue][color=green][color=darkred]
        >>> def f(): print f.func_name[/color][/color][/color]
        ....[color=blue][color=green][color=darkred]
        >>> f()[/color][/color][/color]
        f
        .... as long as 'f' remains bound to that function in the same module, which
        it will unless rebound either from within or without.

        Terry J. Reedy


        Comment

        • anton muhin

          #5
          Re: Name of the function

          Olaf Meding wrote:[color=blue]
          > Is there a way to do this w/o importing another module?
          >
          > Yes this is what I was looking for except the code below requires
          > importing module inspect.[/color]
          I'm afraid that there is no way, at least none I'm aware of (except for
          Terry Reed suggestion, but I think it's not what you are looking for).

          Why you don't want to import a module?

          regards,
          anton.

          Comment

          • Francis Avila

            #6
            Re: Name of the function


            anton muhin wrote in message ...[color=blue]
            >Olaf Meding wrote:[color=green]
            >> Is there a way to do this w/o importing another module?[/color][/color]

            You could use sys instead, which is IIRC initialized at python start-up,
            even though the name isn't put in the namespace until 'import sys'. So
            strictly speaking, it's still importing, but there is no overhead.

            sys._getframe() .f_code.co_name
            [color=blue][color=green]
            >> Yes this is what I was looking for except the code below requires
            >> importing module inspect.[/color]
            >I'm afraid that there is no way, at least none I'm aware of (except for
            >Terry Reed suggestion, but I think it's not what you are looking for).
            >
            >Why you don't want to import a module?[/color]

            I'll second that. I'll even ask why you want the original binding name of
            the function. (When functions are first-class, the "name of the function"
            becomes a bit meaningless.) What are you doing? We might be able to
            suggest something better.

            --
            Francis Avila

            Comment

            Working...