By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,119 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

Accessing func_name from inside a function

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a
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

P: n/a
OK. But that's just as ugly as my attempt.

Mar 25 '06 #4

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.