[Tutor] beginsWith multiple prefixes
Alan Gauld
alan.gauld at btinternet.com
Thu Dec 25 18:46:40 CET 2008
"Kent Johnson" <kent37 at tds.net> wrote
>> You could also make this a method of a subclass of string if you
>> prefer:
>>
>> class MyString(str):
>> def beginswith(self, prefixes):
>> for prefix in prefixes:
>> if self.startswith(prefix):
>> return prefix
>
> It's an appealing idea but the extra step of wrapping strings in
> MyString kind of takes the shine off of it. For example something
> like
> this is awkward:
> for d in os.listdir():
> d = MyString(d)
> if d.beginswith(...):
I would write that as:
for d in os.listdir():
if MyString(d).beginswith(....):
Which isn't that cumbersome. d is still available for
subsequent processing and acces to normal string
methods is still useful , as in:
for d in os.listdir():
if MyString(d).upper().beginswith(....):
But it's a matter of taste I guess.
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
>
> Some languages allow you to extend a built-in class, this technique
> works better there.
>
> Kent
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list