subclassing builtin types

Discussion in 'Python' started by =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Oct 8, 2004.

  1. 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
     
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Oct 8, 2004
    #1
    1. Advertising

  2. BJörn Lindqvist schrieb:
    > There are quite a few other problems I
    > can't solve too, but getting the [] operator right seems almost
    > impossible.


    Since I don't know what other problems you have, I'm just guessing. Maybe this one helps?

    http://www.python.org/2.2.3/descrintro.html#__new__

    Stefan
     
    Stefan Behnel, Oct 8, 2004
    #2
    1. Advertising

  3. BJörn Lindqvist <> wrote:

    > 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.


    ....and that's quite independent from the fact that you're subclassing
    'list'. Whatever you are or aren't subclassing, rebinding an argument
    (including rebinding 'self', be that in an __init__ or any other method)
    never has any observable effects outside that function/method.
    Arguments are local names of a function; they start their life bound to
    some values, but you can freely rebind them, and if you do that only
    affects the current instance of the function itself, period.

    You appear to want to _mutate_ the object to which name 'self' refers,
    rather than _rebind_ the name itself. Therefore, do not assign to
    barename 'self' -- that's just rebinding; use, implicitly or explicitly,
    mutating methods on the object to which name 'self' refers.
    For example, you could assign to self[:] (assignment to slice) or
    equivalently (since self is still empty at that time) call self.extend.

    > 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>.


    I don't do IRC, and if it's true that doing IRC makes one believe that,
    then I'd better keep not doing it.

    > 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?


    If your grasp of Python is so tenuous that you believe that 'self = ...'
    statement could work in any other way than it does, I guess any task
    whatsoever could appear to be very hard. Once you do begin to have a
    modest grasp of Python, however, it's quite easy (neither Java nor C++,
    btw, subclass their List and vector<> types from their builtin arrays:
    *THAT* would be too hard for them...).

    So what's this "almost impossibility" in implementing indexing? Just
    write method __getitem__ for reading from an index (or slice),
    __setitem__ for writing. C++ does not distinguish the two cases (it's
    operator[] either way) and *that* does make things much harder if your
    C++ array-like class needs to do something clever, but that's another
    issue, and not one which Python suffers from.


    Alex
     
    Alex Martelli, Oct 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lonnie Princehouse

    Overriding builtin types

    Lonnie Princehouse, Mar 5, 2004, in forum: Python
    Replies:
    0
    Views:
    366
    Lonnie Princehouse
    Mar 5, 2004
  2. =?iso-8859-1?q?Erik_Wikstr=F6m?=

    Pointer to operator for builtin types

    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 15, 2006, in forum: C++
    Replies:
    6
    Views:
    393
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Dec 15, 2006
  3. Colin J. Williams
    Replies:
    1
    Views:
    277
    Michael Amrhein
    Aug 15, 2007
  4. Colin J. Williams
    Replies:
    5
    Views:
    333
    Colin J. Williams
    Aug 17, 2007
  5. bdb112
    Replies:
    2
    Views:
    317
    Chris Torek
    Jul 2, 2011
Loading...

Share This Page