473,320 Members | 1,856 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,320 software developers and data experts.

Name of the function

I am looking for a way to get at the name of the function (while
executing code inside the function) without (!) knowing the name of
the function.

The code below works, but requires the name of the function. I am
looking for a way to do the same w/o specifying the name of the
function.

def olaf():
# requires name of the function
print olaf.__name__

# this does not work
print self.__name__
Jul 18 '05 #1
5 1501
Olaf Meding wrote:
I am looking for a way to get at the name of the function (while
executing code inside the function) without (!) knowing the name of
the function.

The code below works, but requires the name of the function. I am
looking for a way to do the same w/o specifying the name of the
function.

def olaf():
# requires name of the function
print olaf.__name__

# this does not work
print self.__name__


This seems to work:

def foo():
print inspect.currentframe().f_code.co_name

regards,
anton.
Jul 18 '05 #2
Is there a way to do this w/o importing another module?

Yes this is what I was looking for except the code below requires
importing module inspect.
def foo():
print inspect.currentframe().f_code.co_name


Thanks much for your reply.
Olaf
Jul 18 '05 #3

"anton muhin" <an********@rambler.ru> wrote in message
news:bt************@ID-217427.news.uni-berlin.de...
Olaf Meding wrote:
I am looking for a way to get at the name of the function (while
executing code inside the function) without (!) knowing the name of
the function.

This seems to work:

def foo():
print inspect.currentframe().f_code.co_name


as does this ...
def f(): print f.func_name .... f()

f
.... as long as 'f' remains bound to that function in the same module, which
it will unless rebound either from within or without.

Terry J. Reedy
Jul 18 '05 #4
Olaf Meding wrote:
Is there a way to do this w/o importing another module?

Yes this is what I was looking for except the code below requires
importing module inspect.

I'm afraid that there is no way, at least none I'm aware of (except for
Terry Reed suggestion, but I think it's not what you are looking for).

Why you don't want to import a module?

regards,
anton.
Jul 18 '05 #5

anton muhin wrote in message ...
Olaf Meding wrote:
Is there a way to do this w/o importing another module?
You could use sys instead, which is IIRC initialized at python start-up,
even though the name isn't put in the namespace until 'import sys'. So
strictly speaking, it's still importing, but there is no overhead.

sys._getframe().f_code.co_name
Yes this is what I was looking for except the code below requires
importing module inspect.

I'm afraid that there is no way, at least none I'm aware of (except for
Terry Reed suggestion, but I think it's not what you are looking for).

Why you don't want to import a module?


I'll second that. I'll even ask why you want the original binding name of
the function. (When functions are first-class, the "name of the function"
becomes a bit meaningless.) What are you doing? We might be able to
suggest something better.

--
Francis Avila

Jul 18 '05 #6

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

Similar topics

12
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" ...
11
by: Andrew Thompson | last post by:
I have written a few scripts to parse the URL arguments and either list them or allow access to the value of any parameter by name. <http://www.physci.org/test/003url/index.html>...
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...
5
by: Theresa Hancock via AccessMonster.com | last post by:
I have an Excel table I need to import into Access. The name is entered into one field "Name". I'd like to have two fields in Access, FirstName and LastName. How do I do this. -- Message posted...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
8
by: CES | last post by:
All, I'm sorry but I still don't get this!! I'm trying to use a variable that is passed into a function to to dynamically name an instance of an Object(). I've created a Timer() Object that seems...
31
by: Peter Michaux | last post by:
Hi, I want to know the name of an object's constructor function as a string. Something like this <script type="text/javascript"> function Foo(){}; var a = new Foo(); alert('"' +...
12
by: -Lost | last post by:
What in the world is functionName.name good for? That is: function functionName() { return functionName.name; } I mean, I already had to write out the function's name so it seems that it is...
6
by: Maguila007 | last post by:
Hi Is there any way to obtain the name of the function, inside the function which was called. Ex: function something() { alert( "The name of the function you invoke is " ......should
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.