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

How to refer to the function object itself in the function per se?

P: n/a
When debugging using 'print' statement, I usually want to print some
important values together with the function name as the context of the
values printed out. So my hope is that I could get the name of the
function.

Since every function object actually has a private __name__ attribute
that gives its name, but when I

print __name__

in a function, it usually print the public module-level __name__
attribute, ie, 'main', rather than the function level __name__. So how
could I refer to the function object per se, in the body of the
function itself?

Mar 11 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Sullivan WxPyQtKinter wrote:
So how
could I refer to the function object per se, in the body of the
function itself?

I don't believe you can easily get at the function object, but you can get
at the code object which also has a name (which will be the same as the
function's name unless you've been doing strange things):
import inspect
def f(): print inspect.currentframe().f_code.co_name

f() f

For convenience you might want to put this inside a function:
def myname(): f = inspect.currentframe().f_back
return f.f_code.co_name
def f(): print myname()

f()

f

Mar 11 '06 #2

P: n/a
I have Google the whole thing and find another way for alternative
implementation of getting the function's name. But all they returns are
just strings. If I would like to refer to the function object in order
to call it recursively, what shall I do then?

Mar 11 '06 #3

P: n/a
Sullivan WxPyQtKinter wrote:
I have Google the whole thing and find another way for alternative
implementation of getting the function's name. But all they returns are
just strings. If I would like to refer to the function object in order
to call it recursively, what shall I do then?


a) Learn to quote properly.

b) Use the function's name.
Mar 11 '06 #4

P: n/a
Sullivan WxPyQtKinter wrote:
So how
could I refer to the function object per se, in the body of the
function itself?


Just use the name.

def f():
print f.__name__
f()

f

Mar 11 '06 #5

P: n/a
I am sorry but you misunderstood my idea.
What I want is a generalized method to print out the function name, or
refer to the name of a function. If I use f.__name__, I think I should
just use print "f" to save my keyboard. What I expect is using a
method, or attribute, or another function to get the name of a
function.
Kay Schluehr 写道:
Sullivan WxPyQtKinter wrote:
So how
could I refer to the function object per se, in the body of the
function itself?


Just use the name.

def f():
print f.__name__
f()

f


Mar 11 '06 #6

P: n/a
Sullivan WxPyQtKinter wrote:
If I would like to refer to the function object in order
to call it recursively, what shall I do then?


I think the question is too simple. You can just refer to the function
by its name. Here is an example:
py> def f(start, end):
.... if start >= end:
.... print 'start is end:', start
.... else:
.... print "start increasing to:", start
.... f(start+1, end)
.... print 'leaving f() where start is', start
....
py> f(1,10)
start increasing to: 1
start increasing to: 2
start increasing to: 3
start increasing to: 4
start increasing to: 5
start increasing to: 6
start increasing to: 7
start increasing to: 8
start increasing to: 9
start is end: 10
leaving f() where start is 10
leaving f() where start is 9
leaving f() where start is 8
leaving f() where start is 7
leaving f() where start is 6
leaving f() where start is 5
leaving f() where start is 4
leaving f() where start is 3
leaving f() where start is 2
leaving f() where start is 1

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Mar 11 '06 #7

P: n/a
Sullivan WxPyQtKinter wrote:
I am sorry but you misunderstood my idea.
What I want is a generalized method to print out the function name, or
refer to the name of a function. If I use f.__name__, I think I should
just use print "f" to save my keyboard. What I expect is using a
method, or attribute, or another function to get the name of a
function.


Not exactly:

py> def f():
.... print 'this is function f'
....
py> g = f
py>
py> print g.__name__
f

You might be confusing the idea of a name with the idea of a reference.
It really doesn't matter what you name a function. As long as you have a
reference to a function, or 'callable', you can call it.

py> g()
this is function f

You can even create a reference to a function and call that reference in
the function after the function is defined:

py> def f(start, end):
.... if start >= end:
.... print 'start is end', start
.... else:
.... g(start+1, end)
.... print 'leaving function where start is', start
....
py> g = f
py> f(1,5)
start is end 5
leaving function where start is 5
leaving function where start is 4
leaving function where start is 3
leaving function where start is 2
leaving function where start is 1

But beware re-binding a name in such circumstances:

py> def g(*args):
.... print 'g rebound! args are', args
....
py> f(1,5)
g rebound! args are (2, 5)
leaving function where start is 1

I hope this clears things up.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Mar 11 '06 #8

P: n/a

Sullivan WxPyQtKinter wrote:
I am sorry but you misunderstood my idea.
What I want is a generalized method to print out the function name, or
refer to the name of a function. If I use f.__name__, I think I should
just use print "f" to save my keyboard. What I expect is using a
method, or attribute, or another function to get the name of a
function.


Sorry, if the example was misleading. Here is a better one:

def f(g):
print g.__name__
f(f) f

The __name__ attribute of f is constant and it doesn't matter whether
you pass f around or create aliases:
x = f
x.__name__

f

x refers still to the function object f. Therefore the __name__
attribute does not return "x" but "f". You will always print the right
function name not the name of the local variable.

Kay

Mar 11 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.