Replacing __str__ as never works on any type of object. The type of inline function is not special:
>>> class Foo(object): pass >>> f = Foo() >>> f.__str__ = lambda: 'f' >>> str(f) '<__main__.Foo object at 0x0000000002D13F28>' >>> f.__str__() 'f'
The built-in str does not look for __str__ through the normal search protocol, it passes directly to the class of its argument. Thus, you cannot "override" the __str__ method for a single object; you must set it in the class to apply to all objects of this class. 1
But then again, the built-in function type is not special. You can create a class that behaves basically like a function, and use:
class Function(object): def __init__(self, raw_function): self.raw_function = raw_function def __call__(self, *args, **kwargs): return self.raw_function(*args, **kwargs) def __str__(self): return self.raw_function.__name__
The __call__ method means that you can call objects of this class exactly as if they were functions. This simply passes all the arguments that it receives directly to the main raw function, and returns everything it returns (or throws all the exceptions that it throws). And since this class takes one function as an argument, it is also suitable for a decorator template:
@Function def blah(x): return x + 1
With this:
>>> blah <__main__.Function object at 0x0000000002D13EB8> >>> str(blah) 'blah' >>> blah(33) 34
1 The built-in type of a function is special in some respects (I lied), one of which is that you cannot assign its attributes (that is, the attributes of the class itself, not the attributes of any particular function object). So, before you think, it might be great if you could secure the built-in type of the function so that str works the way you want for all functions that don't work. This is probably the best; modifying things both global and fundamental, since the built-in type of a function will be a great way to come up with some really bizarre errors.