[TIP] Interesting getattr pattern [was: Re: [issue5728] Support telling TestResult objects a test run has finished]

Olemis Lang olemis at gmail.com
Mon Apr 13 07:10:41 PDT 2009


On Sat, Apr 11, 2009 at 1:56 AM, Raphael Marvie <raphael.marvie at lifl.fr> wrote:
> Hi,
>
> Interesting pattern mentioned (Michael tell me if I am wrong):
>
>  startTestRun = getattr(result, 'startTestRun', None)
>  if startTestRun is not None:
>     startTestRun()
>

It's cool ... I see you are using the profiler over there, cool too. I
just wanted to provide another code snippet just to have further alts
considered ;)

What about doing this ?

{{{
#!python

try:
  startTestRun = result.startTestRun
except AttributeError:
  pass
else:
  startTestRun()
}}}

> My question is "what would be the increase in cost of doing an empty
> call compared to the if?" such as:
>

According to my experience, function calls are expensive, especially
in this case where startTestRun method can be called *MANY* times.

-- 
Regards,

Olemis.

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:



More information about the testing-in-python mailing list