Function name unchanged in error message

Chris Rebert clp2 at rebertia.com
Fri Jan 29 15:10:59 CET 2010


On Fri, Jan 29, 2010 at 5:30 AM, andrew cooke <andrew at acooke.org> wrote:
> Is there any way to change the name of the function in an error
> message?  In the example below I'd like the error to refer to bar(),
> for example (the motivation is related function decorators - I'd like
> the wrapper function to give the same name)
>
>>>> def foo():
> ...     return 7
> ...
>>>> foo.__name__ = 'bar'
>>>> foo(123)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: foo() takes no arguments (1 given)

It gets weirder:

>>> print(foo)
<function bar at 0x37b830>

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list