[Tutor] adding methods to native types
Erik Price
erikprice@mac.com
Wed Feb 5 22:46:24 2003
Pardon me for asking such a strange question (perhaps), but is there
any way to add methods to the native types in Python? I wrote my first
Python script in a long time today and I pined for the length() method
of Java's List type -- it took me a while to figure out that there's no
way to determine the length of a list using a list method, you have to
use the len() function.
It led me to wonder if it's possible to subclass the native types in
Python and add additional features. If not, there's nothing to stop
one from composing the list within another class and adding the
additional features, which actually relieves me of the burden of
ensuring that the subclass is completely compatible with the
internal/external implementation of the base class, but I'm curious if
you can do it.
Essentially, I prefer
"string".split("")
to
split("", string)
and wanted to do
list.len()
instead of
len(list)
Thanks,
Erik
--
Erik Price
email: erikprice@mac.com
jabber: erikprice@jabber.org