"Kent Johnson" <[email protected]> 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 - [email protected]
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor