how to get name of function from within function?

Christopher J. Bottaro cjbottaro at alumni.cs.utexas.edu
Sun Jun 5 12:21:09 EDT 2005


Steven Bethard wrote:

> Christopher J. Bottaro wrote:
>> Kent Johnson wrote:
>>>class C(object):
>>>    @in_try
>>>    def func_a(self):
>>>        print "func_a"
>>>        
>>>    @in_try
>>>    def func_b(self):
>>>        print "func_b"
>>>        raise Exception
>>>
>>>You could probably create a metaclass to apply the wrappers automatically
>>>but I like having it explicit as above.
>> 
>> Another good solution, thank you.  Maybe a reason to upgrade to 2.4...=)
> 
> If you can't upgrade, you can write Kent's code like:
> 
> class C(object):
>     def func_a(self):
>         print "func_a"
>     func_a = in_try(func_a)
> 
>     def func_b(self):
>         print "func_b"
>         raise Exception
>     func_b = in_try(func_b)

Yup, @ is shorthand for rebinding the function after applying the arg(s).  I
actually know this stuff, I wonder why I can't think of it on my own...ugh. 
Guess I gotta practice my knowledge more often.

> STeVe

Thanks for the tip,
-- C




More information about the Python-list mailing list