Identifing current function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Donovan

    Identifing current function

    Hi all,

    Is there a way to identify the current function. For example:

    class A:
    def B(self):
    print current_functio n_name


  • Ben Finney

    #2
    Re: Identifing current function

    On Thu, 22 Jan 2004 02:21:54 GMT, Brian Donovan wrote:[color=blue]
    > Is there a way to identify the current function. For example:
    >
    > class A:
    > def B(self):
    > print current_functio n_name[/color]

    Functions, like all objects, have no inherent name. Names are bound to
    objects; each object can have zero, one or more names bound to it. None
    of the names has any particular status as "the" name of the object.

    --
    \ "I moved into an all-electric house. I forgot and left the |
    `\ porch light on all day. When I got home the front door wouldn't |
    _o__) open." -- Steven Wright |
    Ben Finney <http://bignose.squidly .org/>

    Comment

    • Peter Otten

      #3
      Re: Identifing current function

      Brian Donovan wrote:
      [color=blue]
      > Is there a way to identify the current function. For example:[/color]
      [color=blue][color=green][color=darkred]
      >>> import inspect
      >>> def whoami(): return inspect.stack()[1][3][/color][/color][/color]
      ....[color=blue][color=green][color=darkred]
      >>> class A:[/color][/color][/color]
      .... def someMethod(self ): print whoami()
      .... def anotherMethod(s elf): print whoami()
      ....[color=blue][color=green][color=darkred]
      >>> def someFunc():[/color][/color][/color]
      .... print whoami()
      .... a = A()
      .... a.someMethod()
      .... a.anotherMethod ()
      ....[color=blue][color=green][color=darkred]
      >>> someFunc()[/color][/color][/color]
      someFunc
      someMethod
      anotherMethod[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Seems to work. No warranty, though.

      Peter

      Comment

      • Mel Wilson

        #4
        Re: Identifing current function

        In article <slrnc0udgf.flo .bignose-hates-spam@iris.polar .local>,
        Ben Finney <bignose-hates-spam@and-benfinney-does-too.id.au> wrote:[color=blue]
        >On Thu, 22 Jan 2004 02:21:54 GMT, Brian Donovan wrote:[color=green]
        >> Is there a way to identify the current function. For example:
        >>
        >> class A:
        >> def B(self):
        >> print current_functio n_name[/color]
        >
        >Functions, like all objects, have no inherent name. Names are bound to
        >objects; each object can have zero, one or more names bound to it. None
        >of the names has any particular status as "the" name of the object.[/color]

        Functions, classes and modules do have a __name__
        attribute. This is often (but not necessarily) the same as
        the name the thing goes by in a namespace that knows it. It
        might or might not be the name the O.P. wants.

        Regards. Mel.

        Comment

        Working...