How to refer to the function object itself in the function per se?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sullivan WxPyQtKinter

    #1

    How to refer to the function object itself in the function per se?

    When debugging using 'print' statement, I usually want to print some
    important values together with the function name as the context of the
    values printed out. So my hope is that I could get the name of the
    function.

    Since every function object actually has a private __name__ attribute
    that gives its name, but when I

    print __name__

    in a function, it usually print the public module-level __name__
    attribute, ie, 'main', rather than the function level __name__. So how
    could I refer to the function object per se, in the body of the
    function itself?

  • Duncan Booth

    #2
    Re: How to refer to the function object itself in the function per se?

    Sullivan WxPyQtKinter wrote:[color=blue]
    > So how
    > could I refer to the function object per se, in the body of the
    > function itself?
    >
    >[/color]
    I don't believe you can easily get at the function object, but you can get
    at the code object which also has a name (which will be the same as the
    function's name unless you've been doing strange things):
    [color=blue][color=green][color=darkred]
    >>> import inspect
    >>> def f():[/color][/color][/color]
    print inspect.current frame().f_code. co_name

    [color=blue][color=green][color=darkred]
    >>> f()[/color][/color][/color]
    f

    For convenience you might want to put this inside a function:
    [color=blue][color=green][color=darkred]
    >>> def myname():[/color][/color][/color]
    f = inspect.current frame().f_back
    return f.f_code.co_nam e
    [color=blue][color=green][color=darkred]
    >>> def f():[/color][/color][/color]
    print myname()

    [color=blue][color=green][color=darkred]
    >>> f()[/color][/color][/color]
    f

    Comment

    • Sullivan WxPyQtKinter

      #3
      Re: How to refer to the function object itself in the function per se?

      I have Google the whole thing and find another way for alternative
      implementation of getting the function's name. But all they returns are
      just strings. If I would like to refer to the function object in order
      to call it recursively, what shall I do then?

      Comment

      • Duncan Booth

        #4
        Re: How to refer to the function object itself in the function per se?

        Sullivan WxPyQtKinter wrote:
        [color=blue]
        > I have Google the whole thing and find another way for alternative
        > implementation of getting the function's name. But all they returns are
        > just strings. If I would like to refer to the function object in order
        > to call it recursively, what shall I do then?
        >[/color]

        a) Learn to quote properly.

        b) Use the function's name.

        Comment

        • Kay Schluehr

          #5
          Re: How to refer to the function object itself in the function per se?

          Sullivan WxPyQtKinter wrote:
          [color=blue]
          > So how
          > could I refer to the function object per se, in the body of the
          > function itself?[/color]

          Just use the name.

          def f():
          print f.__name__
          [color=blue][color=green][color=darkred]
          >>> f()[/color][/color][/color]
          f

          Comment

          • Sullivan WxPyQtKinter

            #6
            Re: How to refer to the function object itself in the function per se?

            I am sorry but you misunderstood my idea.
            What I want is a generalized method to print out the function name, or
            refer to the name of a function. If I use f.__name__, I think I should
            just use print "f" to save my keyboard. What I expect is using a
            method, or attribute, or another function to get the name of a
            function.
            Kay Schluehr 写道:
            [color=blue]
            > Sullivan WxPyQtKinter wrote:
            >[color=green]
            > > So how
            > > could I refer to the function object per se, in the body of the
            > > function itself?[/color]
            >
            > Just use the name.
            >
            > def f():
            > print f.__name__
            > [color=green][color=darkred]
            > >>> f()[/color][/color]
            > f[/color]

            Comment

            • James Stroud

              #7
              Re: How to refer to the function object itself in the function perse?

              Sullivan WxPyQtKinter wrote:
              If I would like to refer to the function object in order[color=blue]
              > to call it recursively, what shall I do then?[/color]

              I think the question is too simple. You can just refer to the function
              by its name. Here is an example:


              py> def f(start, end):
              .... if start >= end:
              .... print 'start is end:', start
              .... else:
              .... print "start increasing to:", start
              .... f(start+1, end)
              .... print 'leaving f() where start is', start
              ....
              py> f(1,10)
              start increasing to: 1
              start increasing to: 2
              start increasing to: 3
              start increasing to: 4
              start increasing to: 5
              start increasing to: 6
              start increasing to: 7
              start increasing to: 8
              start increasing to: 9
              start is end: 10
              leaving f() where start is 10
              leaving f() where start is 9
              leaving f() where start is 8
              leaving f() where start is 7
              leaving f() where start is 6
              leaving f() where start is 5
              leaving f() where start is 4
              leaving f() where start is 3
              leaving f() where start is 2
              leaving f() where start is 1

              James

              --
              James Stroud
              UCLA-DOE Institute for Genomics and Proteomics
              Box 951570
              Los Angeles, CA 90095


              Comment

              • James Stroud

                #8
                Re: How to refer to the function object itself in the function perse?

                Sullivan WxPyQtKinter wrote:[color=blue]
                > I am sorry but you misunderstood my idea.
                > What I want is a generalized method to print out the function name, or
                > refer to the name of a function. If I use f.__name__, I think I should
                > just use print "f" to save my keyboard. What I expect is using a
                > method, or attribute, or another function to get the name of a
                > function.[/color]

                Not exactly:

                py> def f():
                .... print 'this is function f'
                ....
                py> g = f
                py>
                py> print g.__name__
                f

                You might be confusing the idea of a name with the idea of a reference.
                It really doesn't matter what you name a function. As long as you have a
                reference to a function, or 'callable', you can call it.

                py> g()
                this is function f

                You can even create a reference to a function and call that reference in
                the function after the function is defined:

                py> def f(start, end):
                .... if start >= end:
                .... print 'start is end', start
                .... else:
                .... g(start+1, end)
                .... print 'leaving function where start is', start
                ....
                py> g = f
                py> f(1,5)
                start is end 5
                leaving function where start is 5
                leaving function where start is 4
                leaving function where start is 3
                leaving function where start is 2
                leaving function where start is 1

                But beware re-binding a name in such circumstances:

                py> def g(*args):
                .... print 'g rebound! args are', args
                ....
                py> f(1,5)
                g rebound! args are (2, 5)
                leaving function where start is 1

                I hope this clears things up.

                James

                --
                James Stroud
                UCLA-DOE Institute for Genomics and Proteomics
                Box 951570
                Los Angeles, CA 90095


                Comment

                • Kay Schluehr

                  #9
                  Re: How to refer to the function object itself in the function per se?


                  Sullivan WxPyQtKinter wrote:[color=blue]
                  > I am sorry but you misunderstood my idea.
                  > What I want is a generalized method to print out the function name, or
                  > refer to the name of a function. If I use f.__name__, I think I should
                  > just use print "f" to save my keyboard. What I expect is using a
                  > method, or attribute, or another function to get the name of a
                  > function.[/color]

                  Sorry, if the example was misleading. Here is a better one:

                  def f(g):
                  print g.__name__
                  [color=blue][color=green][color=darkred]
                  >>> f(f)[/color][/color][/color]
                  f

                  The __name__ attribute of f is constant and it doesn't matter whether
                  you pass f around or create aliases:
                  [color=blue][color=green][color=darkred]
                  >>> x = f
                  >>> x.__name__[/color][/color][/color]
                  f

                  x refers still to the function object f. Therefore the __name__
                  attribute does not return "x" but "f". You will always print the right
                  function name not the name of the local variable.

                  Kay

                  Comment

                  Working...