473,397 Members | 2,068 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,397 software developers and data experts.

getting the function name

Is there a way in Python to print the name of the function one is
currently in?
The code

def foo():
debug = True
if (debug):
print "entered",__name__

x = foo()

outputs "entered __main__"

I want to print "entered foo". Of course I could just hard-code the
string "foo" in a print statement, but I don't want to have the
function name written in more than one place.
Jul 18 '05 #1
3 1390
be*******@aol.com wrote:
Is there a way in Python to print the name of the function one is
currently in?

def f(): .... tb = traceback.extract_stack()
.... print tb[-1][2] f()

f

I'm using a more comfortable way by setting a global debug flag and
enabling tracing (see sys.settrace). This gives you the same feature
without adding code to each function.

Mathias
Jul 18 '05 #2
<be*******@aol.com> wrote in message
news:30**************************@posting.google.c om...
Is there a way in Python to print the name of the function one is
currently in?


Google is your friend:

http://groups.google.com/groups?thre...eton.ca&rnum=1

Also
http://groups.google.com/groups?thre...ews.com&rnum=1

--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #3
"Russell Blau" <ru******@hotmail.com> wrote in message
news:2r*************@uni-berlin.de...
<be*******@aol.com> wrote in message
news:30**************************@posting.google.c om...
Is there a way in Python to print the name of the function one is
currently in?
Google is your friend:

http://groups.google.com/groups?thre...eton.ca&rnum=1


Hi.

The posting that the above link refers to was made by me, but I'd like to
suggest that you do not use the code presented there (def WhoMightIBe()).
That was just a bit of a failed experiment to see whether I could track down
the name of a function _after_it_had_been_aliased_. What the OP wants is the
recipe (by Alex Martelli [1]) quoted in the posting I made previous to this
one. I regret posting that experiment now - It was fun to try, but I think
it casts more shadow than light.

Good luck,
Sean

[1] http://aspn.activestate.com/ASPN/Coo...n/Recipe/66062
Jul 18 '05 #4

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

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
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...
4
by: gimme_this_gimme_that | last post by:
Hi, This is sort of a : How to build a Yes/No dialog box qquestion. Or perhaps a question about getting javascript variables from a pop-up window and processing them on a submit. This is...
1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
17
by: Mitko Haralanov | last post by:
I need to be able to get the name of the currently executed method within that method. I know that the method object does have the __name__ attribute but I know know how to access it from withing...
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
5
by: tshad | last post by:
I have the following class in my VS 2008 project that has a namespace of MyFunctions. ********************************* Imports System Imports System.Text.RegularExpressions Namespace...
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
7
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.