gah! I hate the new string syntax
amitp at Xenon.Stanford.EDU
Mon Mar 12 16:41:54 CET 2001
Alex Martelli <aleaxit at yahoo.com> wrote:
| "Amit Patel" <amitp at Xenon.Stanford.EDU> wrote in message
| news:98elrk$li8$1 at nntp.Stanford.EDU...
| > What I mean to say is that I cannot write new methods on strings. So
| Perfectly true, just as you cannot on dictionaries, lists, tuples,
| file objects, and so on. Only classes and extension-classes
| are extensible (by inheritance and overriding), not types.
Even if strings were a class, and I could extend it, it is NOT the
thing to do to add new functionality like capitalize(). Suppose
strings do not have a capitalize method, and I want to add one. And
suppose strings are classes and can be extended. If I wrote:
then I can only capitalize strings I created! I can't take
' '.join(['lister', 'is', 'a', 'smeghead'] and run .capitalize() on
it -- because it's an ordinary string and not a MyString.
The advantage of method functions like string.capitalize is that they
do not require me to change something I can't change (the string
type). I can write capitalize(x) when x is a string. I can't write
x.capitalize() when x is a string.
Defining methods is like running things as root. Yes, you sometimes
need to do it, but it's best to limit yourself to only those things
that need superuser access. Run as much as you can as non-root. And
IMO that goes for methods and functions too -- string type methods are
"root" (where "root" = "Guido & pals") and have access to the
internals of a string, whereas functions on strings are user-level and
go through the abstractions.
P.S. Thanks for reminding me about type != class :-)
Amit J Patel, Computer Science Department, Stanford University
More information about the Python-list