subclassing builtin types

BJörn Lindqvist bjourne at gmail.com
Fri Oct 8 06:23:10 CEST 2004


A problem I have occured recently is that I want to subclass builtin
types. Especially subclassing list is very troublesome to me. But I
can't find the right syntax to use. Take for example this class which
is supposed to be a representation of a genome:

class Genome(list):
    def __init__(self):
        list.__init__(self)
        self = ["A", "G", "C", "G", "A"]
        ....

etc, you get the point. The line self = ["A", "G", "C", "G", "A"] will
not work like I have intended. There are quite a few other problems I
can't solve too, but getting the [] operator right seems almost
impossible. Then I've asked around on IRC and the general consensus
seem to be that subclassing buiiltin types is Not A Good Idea <TM>. 
Why not? To me, it makes perfectly sense to subclass list when you
want to make a class that IS a list with some extra stuff. And in
other languages you do it all the time like Java's List and C++ STL's
vector.  Why is it so much harder in Python?


-- 
mvh Björn



More information about the Python-list mailing list