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

  • Thread starter Sullivan WxPyQtKinter
  • Start date
S

Sullivan WxPyQtKinter

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?
 
D

Duncan Booth

Sullivan said:
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):
print inspect.currentframe().f_code.co_name

f

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

f
 
S

Sullivan WxPyQtKinter

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?
 
D

Duncan Booth

Sullivan said:
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.
 
K

Kay Schluehr

Sullivan said:
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
 
S

Sullivan WxPyQtKinter

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 写é“:
 
J

James Stroud

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/
 
J

James Stroud

Sullivan said:
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/
 
K

Kay Schluehr

Sullivan said:
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

The __name__ attribute of f is constant and it doesn't matter whether
you pass f around or create aliases:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top