startswith( prefix[, start[, end]]) Query

Steve Holden steve at holdenweb.com
Fri Sep 7 02:41:45 EDT 2007


TheFlyingDutchman wrote:
>> Else, you could as well write your own testing function:
>>
>> def str_starts_with(astring, *prefixes):
>>    startswith = astring.startswith
>>    for prefix in prefixes:
>>      if startswith(prefix):
>>        return true
>>    return false
>>
> 
> What is the reason for
>   startswith = astring.startswith
>   startswith(prefix)
> 
> instead of
>   astring.startswith(prefix)
> 
It's an optimization: the assigment creates a "bound method" (i.e. a 
method associated with a specific string instance) and avoids having to 
look up the startswith method of astring for each iteration of the inner 
loop.

Probably not really necessary, though, and they do say that premature 
optimization is the root of all evil ...

regards
Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list