473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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()
Jun 3 '07 #1
2 1216
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?

Jun 3 '07 #2
"Sébastien 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
Jun 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Colin McNaught | last post by:
I'm sure this counts as a newbie question: Take the html expression <a name="thisname" href="newpage.html">GoToIt</a> Inside the <a> tag, 'this' can be used in javascript to refer to the url the...
1
by: tony | last post by:
hi. is there a way to find inside my class.method , what is the class name that call this method ? i know i can send parameter as : Me.GetType.FullName()
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
14
by: Altman | last post by:
Ok I have a control that is inherited from another class. In the child I put msgbox(me.name) in the load event. What always pops up is the name of the parent class and not the name of the...
1
by: thickface | last post by:
Hi all, I have a question and any suggestions for solutions would be greatly appreciated. In my program, I first defined a variable called 'incidence' which has to be updated at every time step, ...
3
by: dcassar | last post by:
I am working on a complex server control that dynamically creates an HtmlInputHidden control that stores its value. As far as the postback process is concerned, this hidden input acts as the...
2
by: Scott | last post by:
If I click the "Update" button on my form below, I can retrieve the value of my combo by using code in the FORM RESULTS section below. Therefore, if I choose the "Inside" option in my combo, I...
5
by: Eliseu Rodrigues | last post by:
Hi I would like to have a static method on a base class that executes some action (for example retrieves the row count) on a table whose name is the same of the inherited class name. For...
12
by: james_027 | last post by:
Hi, for example I have this method def my_method(): # do something # how do I get the name of this method which is my_method here? Thanks,
3
by: Goran Djuranovic | last post by:
Hi all, Is there a way to retrieve a derived class name inside a subroutine or a function of the base class? I am trying to get some data from the database, based on which derived class is calling...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.