how to get name of function from within function?
Christopher J. Bottaro
cjbottaro at alumni.cs.utexas.edu
Sat Jun 4 14:20:01 EDT 2005
Kent Johnson wrote:
...snip...
>> I guess I'm just lazy, but I don't want to write the wrapper func for
>> each
>> new func I want to add. I want it done automatically.
>
> You can do this almost automatically with a decorator:
>
> def in_try(func):
> def wrapper(*args, **kwargs):
> try:
> return func(*args, **kwargs)
> except:
> print "entered except"
> raise
> return wrapper
>
> 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...=)
> Kent
-- C
More information about the Python-list
mailing list