Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

how can I get the name of a method inside it?


I would like to know if it's possible to retrieve the name of a method when
you're inside it. For example, in the following script, I would like to
assign _s so that it prints "you are in method1".

***************************************
class Obj1:
    def __init__(self):
        ...

    def method1(self):
        _s = ???
        print "you are in %s" % _s

x = Obj1()
x.method1()

i'm no expert on the subject, but AFAIK, there's no way to do this.
why not just print "you are in method1"?
what are you exactly trying to do?

"Sbastien Vincent" <sebastien_nimp73<@> wrote:
> I would like to know if it's possible to retrieve the name of a method when
> you're inside it. For example, in the following script, I would like to
> assign _s so that it prints "you are in method1".

> ***************************************
> class Obj1:
>     def __init__(self):
>         ...

>     def method1(self):
>         _s = ???
>         print "you are in %s" % _s

For debugging purposes,

  _s = sys._getframe(0).f_code.co_name

should work (after you've done an "import sys" somewhere appropriate, of
course;-).  The _ in front of _getframe, as well as the klunkiness of it
all, are all indications that this is _not_ recommended for "production
use" -- like most of Python's introspection features, it IS chiefly
meant for debugging purposes.

Alex

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc