Look at the code below:
# mystringfunctions.py
def cap(s):
print s
print "the name of this function is " + "???"
cap ("hello")
Running the code above gives the following output
hello
the name of this function is ???
I would like the output to be
hello
the name of this function is cap
Is that possible ? 16 2005 bo*******@yahoo.com wrote: Look at the code below:
# mystringfunctions.py
def cap(s): print s print "the name of this function is " + "???"
cap ("hello")
Running the code above gives the following output
>>> hello the name of this function is ??? >>> I would like the output to be >>> hello the name of this function is cap >>>
Is that possible ?
Yes, use the moduloe inspect to access the current stack frame:
def handle_stackframe_without_leak():
frame = inspect.currentframe()
try:
print inspect.getframeinfo(frame)[2]
finally:
del frame
Diez bo*******@yahoo.com wrote: Look at the code below:
# mystringfunctions.py
def cap(s): print s print "the name of this function is " + "???"
cap ("hello")
Running the code above gives the following output
>>> hello the name of this function is ??? >>> I would like the output to be >>> hello the name of this function is cap >>>
Is that possible ?
Yes, use the moduloe inspect to access the current stack frame:
def handle_stackframe_without_leak():
frame = inspect.currentframe()
try:
print inspect.getframeinfo(frame)[2]
finally:
del frame
Diez bo*******@yahoo.com wrote:
[edited slightly] def cap(): print "the name of this function is " + "???" cap ()
sys._getframe() would help you here: import sys sys._getframe()
<frame object at 0x00B496D0> def f():
.... global x
.... x = sys._getframe()
.... f() x
<frame object at 0x00B15250> dir(x)
[..., 'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', ...] dir(x.f_code)
[...'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames'] x.f_code.co_name
'f'
So your function could be:
import sys def cap():
.... print 'function name is', sys._getframe().f_code.co_name
.... cap()
function name is cap
-Peter bo*******@yahoo.com wrote:
[edited slightly] def cap(): print "the name of this function is " + "???" cap ()
sys._getframe() would help you here: import sys sys._getframe()
<frame object at 0x00B496D0> def f():
.... global x
.... x = sys._getframe()
.... f() x
<frame object at 0x00B15250> dir(x)
[..., 'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', ...] dir(x.f_code)
[...'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames'] x.f_code.co_name
'f'
So your function could be:
import sys def cap():
.... print 'function name is', sys._getframe().f_code.co_name
.... cap()
function name is cap
-Peter
Thanks Diez and Peter,
Just what I was looking for. In "Library Reference" heading
3.11.1 Types and members
I found the info about the method you described. I also made a little
function to print out not just the name of the function but also the
parameter list. Here it is
# fname.py
import sys, string
def cap(s, n):
print string.replace("".join([sys._getframe().f_code.co_name, \
repr(sys._getframe().f_code.co_varnames)]), "\'", "")
cap('Hello', 'Bob')
Running this yields the result
cap(s, n)
Thanks Diez and Peter,
Just what I was looking for. In "Library Reference" heading
3.11.1 Types and members
I found the info about the method you described. I also made a little
function to print out not just the name of the function but also the
parameter list. Here it is
# fname.py
import sys, string
def cap(s, n):
print string.replace("".join([sys._getframe().f_code.co_name, \
repr(sys._getframe().f_code.co_varnames)]), "\'", "")
cap('Hello', 'Bob')
Running this yields the result
cap(s, n)
Decorate any function with @aboutme(), which
will print the function name each time the function is called.
All the 'hello' stuff is in the aboutme() decorator code.
There is no code in the decorated functions themselves
doing anything to telling us the function name.
# The decorator
def aboutme():
def thecall(f, *args, **kwargs):
# Gets to here during module load of each decorated function
def wrapper( *args, **kwargs):
# Our closure, executed when the decorated function is called
print "Hello\nthe name of this function is '%s'\n" \
% f.func_name
return f(*args, **kwargs)
return wrapper
return thecall
@aboutme()
def testing(s):
print "string '%s' is argument for function" % s
@aboutme()
def truing():
return True
# Try these
testing('x')
testing('again')
truing()
Decorate any function with @aboutme(), which
will print the function name each time the function is called.
All the 'hello' stuff is in the aboutme() decorator code.
There is no code in the decorated functions themselves
doing anything to telling us the function name.
# The decorator
def aboutme():
def thecall(f, *args, **kwargs):
# Gets to here during module load of each decorated function
def wrapper( *args, **kwargs):
# Our closure, executed when the decorated function is called
print "Hello\nthe name of this function is '%s'\n" \
% f.func_name
return f(*args, **kwargs)
return wrapper
return thecall
@aboutme()
def testing(s):
print "string '%s' is argument for function" % s
@aboutme()
def truing():
return True
# Try these
testing('x')
testing('again')
truing() bo*******@yahoo.com writes: Thanks Diez and Peter, Just what I was looking for. In "Library Reference" heading 3.11.1 Types and members Running this yields the result
cap(s, n)
You've now got three solutions. They'll work fine most of the time,
but can't be trusted in general. Binding a name to a function doesn't
change the name that these solutions return, and the name they return
may no longer be bound to said function. Just a warning.
<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. bo*******@yahoo.com writes: Thanks Diez and Peter, Just what I was looking for. In "Library Reference" heading 3.11.1 Types and members Running this yields the result
cap(s, n)
You've now got three solutions. They'll work fine most of the time,
but can't be trusted in general. Binding a name to a function doesn't
change the name that these solutions return, and the name they return
may no longer be bound to said function. Just a warning.
<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
On Sat, 19 Nov 2005 23:30:32 -0500, Mike Meyer <mw*@mired.org> wrote: bo*******@yahoo.com writes: Thanks Diez and Peter, Just what I was looking for. In "Library Reference" heading 3.11.1 Types and members Running this yields the result
cap(s, n)
You've now got three solutions. They'll work fine most of the time, but can't be trusted in general. Binding a name to a function doesn't change the name that these solutions return, and the name they return may no longer be bound to said function. Just a warning.
But the one buried in co_name seems to persist
(barring byte code munging in the decorator ;-) def fren(newname='newname'):
... def fren(f):
... f.__name__ = newname
... return f
... return fren
... @fren('bar')
... def foo():pass
...
Could have done that manually, but just playing.
Ok, rebind foo and remove the old name, for grins baz = foo del foo
See what we've got dir()
['__builtins__', '__doc__', '__name__', 'baz', 'fren']
Check name(s) ;-)
Local binding to the function object first: baz
<function bar at 0x02EEADF4>
Its outer name: baz.func_name
'bar'
Its def name: baz.func_code.co_name
'foo'
Regards,
Bengt Richter
On Sat, 19 Nov 2005 23:30:32 -0500, Mike Meyer <mw*@mired.org> wrote: bo*******@yahoo.com writes: Thanks Diez and Peter, Just what I was looking for. In "Library Reference" heading 3.11.1 Types and members Running this yields the result
cap(s, n)
You've now got three solutions. They'll work fine most of the time, but can't be trusted in general. Binding a name to a function doesn't change the name that these solutions return, and the name they return may no longer be bound to said function. Just a warning.
But the one buried in co_name seems to persist
(barring byte code munging in the decorator ;-) def fren(newname='newname'):
... def fren(f):
... f.__name__ = newname
... return f
... return fren
... @fren('bar')
... def foo():pass
...
Could have done that manually, but just playing.
Ok, rebind foo and remove the old name, for grins baz = foo del foo
See what we've got dir()
['__builtins__', '__doc__', '__name__', 'baz', 'fren']
Check name(s) ;-)
Local binding to the function object first: baz
<function bar at 0x02EEADF4>
Its outer name: baz.func_name
'bar'
Its def name: baz.func_code.co_name
'foo'
Regards,
Bengt Richter
"B Mahoney" wrote: Decorate any function with @aboutme(), which will print the function name each time the function is called.
All the 'hello' stuff is in the aboutme() decorator code. There is no code in the decorated functions themselves doing anything to telling us the function name.
so you've moved a trivial print statement from the function itself into
a decorator, so you can add an extra line before the function instead
of inside it. wow.
</F> bo*******@yahoo.com wrote: Look at the code below:
# mystringfunctions.py
def cap(s): print s print "the name of this function is " + "???"
cap ("hello")
Running the code above gives the following output
>>> hello the name of this function is ??? >>> I would like the output to be >>> hello the name of this function is cap >>>
Is that possible ?
def cap(s):
print s
print "the name of this function is " + "cap"
yes, I'm serious.
if your question had been "can a function figure out the name of the
function that called it", the answer would have been different.
</F>
"B Mahoney" wrote: Decorate any function with @aboutme(), which will print the function name each time the function is called.
All the 'hello' stuff is in the aboutme() decorator code. There is no code in the decorated functions themselves doing anything to telling us the function name.
so you've moved a trivial print statement from the function itself into
a decorator, so you can add an extra line before the function instead
of inside it. wow.
</F> bo*******@yahoo.com wrote: Look at the code below:
# mystringfunctions.py
def cap(s): print s print "the name of this function is " + "???"
cap ("hello")
Running the code above gives the following output
>>> hello the name of this function is ??? >>> I would like the output to be >>> hello the name of this function is cap >>>
Is that possible ?
def cap(s):
print s
print "the name of this function is " + "cap"
yes, I'm serious.
if your question had been "can a function figure out the name of the
function that called it", the answer would have been different.
</F> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Penn Markham |
last post by:
Hello all,
I am writing a script where I need to use the system() function to call
htpasswd. I can do this just fine on the command line...works great
(see attached file, test.php). When my...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Maroon |
last post by:
Hi,
I want to write a standard java user defined function, like this
example:-
select db_fun("roll"), roll from student;
**db_fun() will work for all tables of the all databse
here db_fun is...
|
by: JC |
last post by:
The following function creates a display name from a and
and . I call the function from a query. When I
run the query those records that have BOTH a his and her name appear
correctly, but if...
|
by: Bill Rubin |
last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
|
by: Ram |
last post by:
Hi Friends
I want to develope a custom control in .net which can be used with any
project. I am writing a function in that class which I want to take any
object as parameter. For that I have...
|
by: Serious_Practitioner |
last post by:
Good day, and thank you in advance for any assistance. I'm having trouble
with something that I'm trying for the first time.
Using Access 2000 - I want to run a function either on the click of a...
|
by: jj6849 |
last post by:
I have been using the dom to add a row to my form for awhile now, but now I need to do some validation to make sure certain check boxes aren't checked with other check boxes. Now of course it works...
|
by: wugon.net |
last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad
query performance
Env:
db2 LUW V8 + FP14
Problem :
We have history data from 2005/01/01 ~ 2007/05/xx in single big...
|
by: Bryan Parkoff |
last post by:
I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope....
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |