strip() using strings instead of chars

Christoph Zwerschke cito at online.de
Fri Jul 11 16:45:20 CEST 2008


Bruno Desthuilliers schrieb:
> DRY/SPOT violation. Should be written as :
> 
>  prefix = 'http://'
>  if url.startswith(prefix):
>      url = url[len(prefix):]

That was exactly my point. This formulation is a bit better, but it 
still violates DRY, because you need to type "prefix" two times. It is 
exactly this idiom that I see so often and that I wanted to simplify. 
Your suggestions work, but I somehow feel such a simple task should have 
a simpler formulation in Python, i.e. something like

url = url.lstripstr(('http://', 'https://'))

instead of

for prefix in ('http://', 'https://'):
     if url.startswith(prefix):
         url = url[len(prefix):]
         break

-- Christoph



More information about the Python-list mailing list