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

Accessing func_name from inside a function

I'd like to access the name of a function from inside the function. My
first idea didn't work.
def foo(): .... print func_name
.... foo() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in foo
NameError: global name 'func_name' is not defined

My second attempt works but looks ugly.
def foo(): .... import inspect
.... print inspect.stack()[0][3]
.... foo()

foo

Is there a standard way of getting the name of a function from inside
the function?

Mar 25 '06 #1
14 3856
James Thiele wrote:
Is there a standard way of getting the name of a function from inside
the function?


No, there isn't.

Martin
Mar 25 '06 #2
James Thiele wrote:
I'd like to access the name of a function from inside the function.


http://aspn.activestate.com/ASPN/Coo...n/Recipe/66062

Kent
Mar 25 '06 #3
OK. But that's just as ugly as my attempt.

Mar 25 '06 #4
"James Thiele" <ja****************@gmail.com> writes:
I'd like to access the name of a function from inside the function.


A function, like most other objects in Python, can have any number of
names bound to it without the object being informed. Any of those
names can then be used to reference the object, and the object has no
way of knowing by what name it was referenced.

Because of this fallacy, it's generally considered bad programming
style to want to know "the" name of the current object from inside
that object.

What is it you're trying to achieve?

--
\ "Unix is an operating system, OS/2 is half an operating system, |
`\ Windows is a shell, and DOS is a boot partition virus." -- |
_o__) Peter H. Coffin |
Ben Finney

Mar 26 '06 #5
On Sun, 26 Mar 2006 10:19:36 +1000, Ben Finney wrote:
"James Thiele" <ja****************@gmail.com> writes:
I'd like to access the name of a function from inside the function.
A function, like most other objects in Python, can have any number of
names bound to it without the object being informed. Any of those
names can then be used to reference the object, and the object has no
way of knowing by what name it was referenced.

Because of this fallacy,


"You keep using that word. I do not think it means what you think it
means."

*wink*
it's generally considered bad programming
style to want to know "the" name of the current object from inside
that object.


I agree whole-heartedly with Ben's sentiments here, although I can also
point out by example why it might be useful for a function to know it's
own name:

def Ackermann(i, j):
"""Returns the Ackermann function of integers i and j.

The Ackermann function is an example of a function which grows at a
much faster than exponential rate. Ackermann(i, j) for i > 2 grows
even more quickly than 2**2**2**...**2 (where there are j exponents).
It is an example of a recursive function which is not primitively
recursive and was discovered by the German mathematician Wilhelm
Ackermann in 1928.
Ackermann(1, 1) 2 Ackermann(2, 2) 16 Ackermann(2, 3) 65536

The Ackermann function is not defined for i,j <= 0.

See http://en.wikipedia.org/wiki/Ackermann_function
and 'Introduction to Algorithms' by Thomas H Cormen, Charles E
Leiserson, Ronald L Rivest, pp. 451-453.
"""
if i < 0 or j < 0:
raise ValueError(
"arguments to the Ackermann function must be positive")
if i == 1:
return 2**j
if j == 1:
return Ackermann(i-1, 2)
return Ackermann(i-1, Ackermann(i, j-1))

Notice that if you change the name of the function, you have to change it
in no less than three places in the function definition and four places
in the __doc__ string, but a global search and replace is too greedy and
will change too much. As a general principle, it is considered best
practice to code in such a way that if you change something, you only need
to change it in one place.

I guess that the Original Poster wants some magic that allows functions to
do this:

def Ackermann(i, j):
"""Returns the Ackermann function of integers i and j.

The Ackermann function is an example of a function which grows at a
much faster than exponential rate. %MAGIC(i, j) for i > 2 grows
even more quickly than 2**2**2**...**2 (where there are j exponents).
It is an example of a recursive function which is not primitively
recursive and was discovered by the German mathematician Wilhelm
Ackermann in 1928.
%MAGIC(1, 1) 2 %MAGIC(2, 2) 16 %MAGIC(2, 3)

65536

The Ackermann function is not defined for i,j <= 0.

See http://en.wikipedia.org/wiki/Ackermann_function
and 'Introduction to Algorithms' by Thomas H Cormen, Charles E
Leiserson, Ronald L Rivest, pp. 451-453.
"""
if i < 0 or j < 0:
raise ValueError(
"arguments to the Ackermann function must be positive")
if i == 1:
return 2**j
if j == 1:
return %MAGIC(i-1, 2)
return %MAGIC(i-1, %MAGIC(i, j-1))
Now he can easily rename "Ackermann" to "Ack" and need only make the
change in one place.

I suspect that the correct solution to the O.P.'s problem is to think
carefully about the names of your functions in advance.

--
Steven.

Mar 26 '06 #6
Ben Finney <bi****************@benfinney.id.au> wrote:
"James Thiele" <ja****************@gmail.com> writes:
I'd like to access the name of a function from inside the function.


A function, like most other objects in Python, can have any number of
names bound to it without the object being informed. Any of those


Yes, but, quite independent of the 0+ names BOUND to the object,
function objects, like class and module objects, have ONE "true" name
(which may or may not coincide with one of those bound to the object, if
any) -- the special attribute __name__ (AKA func_name for function
objects only, but I'll stick with __name__ which applies uniformly).

Code in a module can get the module's name by simply accessing global
variable __name__. I see nothing untowards in a desire to access
__name__ from code within a function or a class body, getting the name
of the function or the class respectively rather than the name of the
module they're in... it's just not implemented that way (and can't be
changed until Python 3.0 for backwards compatibility). But proposals for
3.0 can be entertained.

Personally, I'd rather have a 3.0 keyword referring to "the current
object" (module for module toplevel code, class for classbody toplevel
code, function for code within a function) -- say for the sake of
argument the keyword is 'current'; this would allow current.__name__ to
have the obvious meaning, and would also allow a few more neat things
(such as natural access to current.__doc__).

But if the OP is looking for a Python 2.* solution, then sys._getframe
or module inspect are the only way to go, be they "ugly" as he considers
them, or not.
Alex
Mar 26 '06 #7
Alex Martelli wrote:
Personally, I'd rather have a 3.0 keyword referring to "the current
object" (module for module toplevel code, class for classbody toplevel
code, function for code within a function) -- say for the sake of
argument the keyword is 'current'; this would allow current.__name__ to
have the obvious meaning, and would also allow a few more neat things
(such as natural access to current.__doc__).


That was my thought too, that what is really desired is a *obvious* way
to get a dependable reference to the current function as you describe here.

A quick experiment... with an obvious result:
def foo(): print foo.__name__ ... foo() foo boo = foo
boo() foo boo.__name__ 'foo' foo = None
boo()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in foo
AttributeError: 'NoneType' object has no attribute '__name__'


A "Current" key word would fix this. Or possibly "This" which would be
short for "This object".

This may also relate to suggestions to reduce the need for having self
in the argument list of methods. So if a keyword "This" could mean this
method or function, then "Self" could mean this class. Hmmm, methods
that use "Self" in this way might run into problems, but I havn't had
enough coffee to think it though. ;-)

Cheers,
Ron

Mar 26 '06 #8
James Thiele a écrit :
I'd like to access the name of a function from inside the function. My
first idea didn't work.

def foo():
... print func_name
...
foo()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in foo
NameError: global name 'func_name' is not defined

My second attempt works but looks ugly.

def foo():
... import inspect
... print inspect.stack()[0][3]
...
foo()


foo

Is there a standard way of getting the name of a function from inside
the function?


You've already got good answers. Now here's another workaround:

class _FooFunction(object):
def __call__(self):
print self.__class__.__name__
foo = _FooFunction()

Mar 26 '06 #9
Ron Adam <rr*@ronadam.com> wrote:
A "Current" key word would fix this. Or possibly "This" which would be
short for "This object".


I think "This" would cause huge confusion, since in other popular
language the keyword "this" means (a pointer/reference to) "the instance
variable on which the method is being called" -- my use is instead for
"the object of the immediate lexically enclosing scope" (a module,
class, or function). Implementation wouldn't be trivial in today's
CPython, anyway: frames have no references at all to function objects
(only to _code_ objects), and class objects don't even exist untill well
after their top-level code has long finished running; to make Current
possible, these subtle points of semantics would have to be changed in
quite a revolutionary way, especially where classes are concerned.
Alex
Mar 26 '06 #10
James Thiele <ja****************@gmail.com> wrote:
I'd like to access the name of a function from inside the function. My
first idea didn't work.
def foo(): ... print func_name
... foo()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in foo
NameError: global name 'func_name' is not defined


So, define it -- as a function, of course:

def func_name():
import sys
return sys._getframe(1).f_code.co_name

def foo():
print func_name()

def bar():
print func_name()

No doubt you'll object that the internals of func_name are "ugly", but
the point is that it can be hidden anywhere you like, so its internals
are as irrelevant as they would be if it was a built-in.
Alex
Mar 26 '06 #11
Steven D'Aprano wrote:
On Sun, 26 Mar 2006 10:19:36 +1000, Ben Finney wrote:
"James Thiele" <ja****************@gmail.com> writes:
I'd like to access the name of a function from inside the function.
A function, like most other objects in Python, can have any number of
names bound to it without the object being informed. Any of those
names can then be used to reference the object, and the object has no
way of knowing by what name it was referenced.

Because of this fallacy,


"You keep using that word. I do not think it means what you think it
means."

*wink*
it's generally considered bad programming
style to want to know "the" name of the current object from inside
that object.


I agree whole-heartedly with Ben's sentiments here, although I can also
point out by example why it might be useful for a function to know it's
own name:

def Ackermann(i, j):
"""Returns the Ackermann function of integers i and j.

The Ackermann function is an example of a function which grows at a
much faster than exponential rate. Ackermann(i, j) for i > 2 grows
even more quickly than 2**2**2**...**2 (where there are j exponents).
It is an example of a recursive function which is not primitively
recursive and was discovered by the German mathematician Wilhelm
Ackermann in 1928.
>>> Ackermann(1, 1) 2 >>> Ackermann(2, 2) 16 >>> Ackermann(2, 3) 65536

The Ackermann function is not defined for i,j <= 0.

See http://en.wikipedia.org/wiki/Ackermann_function
and 'Introduction to Algorithms' by Thomas H Cormen, Charles E
Leiserson, Ronald L Rivest, pp. 451-453.
"""
if i < 0 or j < 0:
raise ValueError(
"arguments to the Ackermann function must be positive")
if i == 1:
return 2**j
if j == 1:
return Ackermann(i-1, 2)
return Ackermann(i-1, Ackermann(i, j-1))

Notice that if you change the name of the function, you have to change it
in no less than three places in the function definition and four places
in the __doc__ string, but a global search and replace is too greedy and
will change too much. As a general principle, it is considered best
practice to code in such a way that if you change something, you only need
to change it in one place.

I guess that the Original Poster wants some magic that allows functions to
do this:

def Ackermann(i, j):
"""Returns the Ackermann function of integers i and j.

The Ackermann function is an example of a function which grows at a
much faster than exponential rate. %MAGIC(i, j) for i > 2 grows
even more quickly than 2**2**2**...**2 (where there are j exponents).
It is an example of a recursive function which is not primitively
recursive and was discovered by the German mathematician Wilhelm
Ackermann in 1928.
>>> %MAGIC(1, 1) 2 >>> %MAGIC(2, 2) 16 >>> %MAGIC(2, 3)

65536

The Ackermann function is not defined for i,j <= 0.

See http://en.wikipedia.org/wiki/Ackermann_function
and 'Introduction to Algorithms' by Thomas H Cormen, Charles E
Leiserson, Ronald L Rivest, pp. 451-453.
"""
if i < 0 or j < 0:
raise ValueError(
"arguments to the Ackermann function must be positive")
if i == 1:
return 2**j
if j == 1:
return %MAGIC(i-1, 2)
return %MAGIC(i-1, %MAGIC(i, j-1))


Another possibility would be:

def recursive_func(func):
def new_func(*args, **kw):
return func(new_func, *args, **kw)
return new_func

@recursive_func
def Ackermann(recurse, i, j):
# yadda yadda
recurse(i-1, ...), recurse(y-2, ...)
Now he can easily rename "Ackermann" to "Ack" and need only make the
change in one place.

I suspect that the correct solution to the O.P.'s problem is to think
carefully about the names of your functions in advance.


By the way, the "real" problem here is referencing by name, rather than
using "true" references. Which is the result of using a textual language.
The "real" solution would be to store real-references to the function and
only present the name in a graphical interface.
Mar 26 '06 #12
Alex Martelli wrote:
Ron Adam <rr*@ronadam.com> wrote:
A "Current" key word would fix this. Or possibly "This" which would be
short for "This object".


I think "This" would cause huge confusion, since in other popular
language the keyword "this" means (a pointer/reference to) "the instance
variable on which the method is being called" -- my use is instead for
"the object of the immediate lexically enclosing scope" (a module,
class, or function). Implementation wouldn't be trivial in today's
CPython, anyway: frames have no references at all to function objects
(only to _code_ objects), and class objects don't even exist untill well
after their top-level code has long finished running; to make Current
possible, these subtle points of semantics would have to be changed in
quite a revolutionary way, especially where classes are concerned.
Alex


Yes, I figured it would not be an easy or simple addition. An easier
alternative might be to be able to limit specific name rebindings. Then
some names could be depended on to always point to a specific object.
Something like that would need to be used sparingly I think.

Cheers,
Ron
Mar 26 '06 #13
Eyal Lotem wrote:
By the way, the "real" problem here is referencing by name, rather than
using "true" references. Which is the result of using a textual language.
The "real" solution would be to store real-references to the function and
only present the name in a graphical interface.


There is a problem with this attitude (to which I used to subscribe).
Just as what constitutes a mathematical proof is a social agreement
among mathematicians, programmers need to be able to read programs
by other programmers. People do not communicate in data structures.
I worked with LML, a lazy ML variant that (among other things) allowed
specification of the print form of structures. I wound up debugging
(for a nasty 12 hours) a program which printed identically to the
correct program, but was not, in fact, the correct program. That one
experience has convinced me that programs are defined in print, not
structures.

--Scott David Daniels
sc***********@acm.org
Mar 27 '06 #14
Scott David Daniels wrote:
Eyal Lotem wrote:
By the way, the "real" problem here is referencing by name, rather than
using "true" references. Which is the result of using a textual language.
The "real" solution would be to store real-references to the function and
only present the name in a graphical interface.


There is a problem with this attitude (to which I used to subscribe).
Just as what constitutes a mathematical proof is a social agreement
among mathematicians, programmers need to be able to read programs
by other programmers. People do not communicate in data structures.
I worked with LML, a lazy ML variant that (among other things) allowed
specification of the print form of structures. I wound up debugging
(for a nasty 12 hours) a program which printed identically to the
correct program, but was not, in fact, the correct program. That one
experience has convinced me that programs are defined in print, not
structures.


Well, what you describe is a hidden semantic difference, which is a problem
of the view, not of the representation. As long as the view can display
the details of the program in their entirety, text has no advantage in this
aspect. There is no reason why replacing the textual backend with a richer
object model and true references would prevent a simple viewer that renders
the programs as simple text similar to the way they look today. It would
seem almost equivalent but suddenly easy renames, diff tracking for source
control, and semantic editing would not only be possible but easy.

Mar 27 '06 #15

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

Similar topics

1
by: Demian | last post by:
Hi, I developed an OCX in VC++, wich takes a VARIANT as parameter and return a Long as the result of the operation. This VARIANT cames from a client VB6.0 wich send an array of Byte as a VARIANT,...
18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
4
by: Barry Mossman | last post by:
Hi, I am throwing an exception derived from ApplicationException as follows catch (Exception ex) { throw new MyException("message", ex); } My exception's constructor is: public...
4
by: dogpuke | last post by:
I have a class CString. I'm wondering if it's possible to make a global function mystr_cat that does this: CString s1 = "hello"; s1 = mystr_cat("another", "string", "here"); Thus mystr_cat...
5
by: Alex Maghen | last post by:
Hi. If I create a WebControl (User Control, actually), I know how, easily, to access the design-time Properties that have been set as Propertiy nodes in the tag used on the ASPX page. But I've...
1
by: Dave | last post by:
I have the following ASP.NET 2.0 code (simplified here for ease): <asp:Repeater id="SearchResultsRepeater" runat="server"> <ItemTemplate> <uc:SearchResult ID="SearchResult"...
6
by: smoermeli | last post by:
#include <iostream> #include <vector> class Thing { private: int value; friend class Person; public: int getValue() { return value; } void...
1
by: Hunter | last post by:
I am writing a script that needs to send some emails. And I've used smtplib in the past and it is pretty easy. But I thought, gee it would be easier if I could just call it as a function, passing...
0
by: gaya3 | last post by:
Hi, For our swing application we are using derby database. Since its desktoop application, we are providing the application as jar to the clients. But issue is in finding the database. Derby...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
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
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...

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.