how to get name of function from within function?
Christopher J. Bottaro
cjbottaro at alumni.cs.utexas.edu
Fri Jun 3 18:40:34 EDT 2005
Steven Bethard wrote:
> Christopher J. Bottaro wrote:
>> I want to get the name of the function from within the function.
>> Something like:
>>
>> def myFunc():
>> print __myname__
>>
>>>>> myFunc()
>> 'myFunc'
>
> There's not a really good way to do this. Can you give some more detail
> on what exactly you're trying to do here?
I want to make wrappers around functions but I can't use __getattr__ because
the functions are being published as a SOAP service and the SOAP lib I'm
using requires a callable object as the argument to the publishing
function.
Basically I want to wrap every function in try/except automatically.
Typically, I suppose people would do that with __getattr__. The way my
code is now is that every function (err, method) is enclosed in the exact
same try/except error handling code. Everytime I make a new function, I
copy/paste that try/catch block. Its ugly, it clutters the code, its prone
to error, and if i decide to change the error handling code later, I have
to change tons of functions.
Actually, I just realized that function call arguments are not passed to
__getattr__, so my idea of wrapping function calls that way won't work.
Is there something like PHP's __call() method in Python?
>> Also, is there a way to turn normal positional args into a tuple without
>> using *? Like this:
>>
>> def f(a, b, c):
>> print get_args_as_tuple()
>>
>>>>>f(1, 2, 3)
>>
>> (1, 2, 3)
>
> Um...
>
> py> def f(a, b, c):
> ... print (a, b, c)
> ...
> py> f(1, 2, 3)
> (1, 2, 3)
>
> ?
In your method, if the name and/or number of positional arguments changes,
the body of the function must change. I want to avoid that.
Thanks.
More information about the Python-list
mailing list