Is it possible to make a unittest decorator to rename a method from "x" to "testx?"

Terry Reedy tjreedy at udel.edu
Thu Aug 8 15:28:40 EDT 2013


On 8/8/2013 12:20 PM, adam.preble at gmail.com wrote:
> On Thursday, August 8, 2013 3:04:30 AM UTC-5, Terry Reedy wrote:

>> def test(f):
>>
>>       f.__class__.__dict__['test_'+f.__name__]

Sorry, f.__class__ is 'function', not the enclosing class. A decorator 
for a method could not get the enclosing class name until 3.3, when it 
would be part of f.__qualname__.

Use one of the other suggestions.

> Just for giggles I can mess around with those exact lines, but I did get spanked trying to do something similar.  I couldn't reference __class__ for some reason (Python 2.7 problem?).

In 2.x, old-style classes and instances thereof do not have .__class__. 
All other objects do, as far as I know.

-- 
Terry Jan Reedy




More information about the Python-list mailing list