how to get name of function from within function?

Christopher J. Bottaro cjbottaro at alumni.cs.utexas.edu
Mon Jun 6 12:59:26 EDT 2005


<posted & mailed>

Hey again Steven,
  I'm still having problems...

Steven Bethard wrote:

> Something like this might work:
> 
> py> class C(object):
> ...     def func_a(self):
> ...         print "func_a"
> ...     def func_b_impl(self):
> ...         print "func_b"
> ...         raise Exception
> ...     def __getattr__(self, name):
> ...         func = getattr(self, '%s_impl' % name)
> ...         wrapped_func = self._impl_wrapper(func)
> ...         setattr(self, name, wrapped_func)
> ...         return wrapped_func
> ...     def _impl_wrapper(self, func):
> ...         def wrapper(*args, **kwargs):
> ...             try:
> ...                 return func(*args, **kwargs)
> ...             except:
> ...                 print "entered except"
> ...                 raise
> ...         return wrapper
> ...
> py> c = C()
> py> c.func_a()
> func_a
> py> c.func_b()
> func_b
> entered except
> Traceback (most recent call last):
>    File "<interactive input>", line 1, in ?
>    File "<interactive input>", line 15, in wrapper
>    File "<interactive input>", line 6, in func_b_impl
> Exception
> 
> The idea here is that __getattr__ is called whenever the class doesn't
> have a particular function.  The __getattr__ method then tries to find a
> corresponding _impl function, wraps it with appropriate try/except code,
> and returns the wrapped function.

The problem is:
>>> c.func_b.__name__
'wrapper'

That messes up SOAPpy's RegisterFunction() method which apparently depends
on the __name__ of the function to publish it as an available SOAP
function.

Any suggestions on how to change the name of c.func_b to 'func_b' instead of
'wrapper'?
 
> HTH,
> STeVe

Thanks a bunch,
-- C




More information about the Python-list mailing list