conditional running of code portion

Dieter Maurer dieter at handshake.de
Mon Aug 6 13:02:24 EDT 2012


Serhiy Storchaka <storchaka at gmail.com> writes:

> On 05.08.12 09:30, Steven D'Aprano wrote:
>> If you are working in a tight loop, you can do this:
>>
>> if VERBOSE_FLAG:
>>      for item in loop:
>>          print(DEBUG_INFORMATION)
>>          do_actual_work(item)
>> else:
>>      for item in loop:
>>          do_actual_work(item)
>
> Or this:
>
> if VERBOSE_FLAG:
>     def do_work(item):
>         print(DEBUG_INFORMATION)
>         do_actual_work(item)
> else:
>     do_work = do_actual_work
>
> for item in loop:
>     do_work(item)

Be warned: a function call is *much* more expensive than an
"if variable:".





More information about the Python-list mailing list