Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subclassing builtin types

Reply
Thread Tools

subclassing builtin types

 
 
=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=
Guest
Posts: n/a
 
      10-08-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
 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      10-08-2004
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
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      10-08-2004
BJörn Lindqvist <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subtle error slows code by 10x (builtin sum()) - replace builtin sumwithout using import? bdb112 Python 2 07-02-2011 03:13 AM
Re: sub-classing the types in the builtin module datetime Colin J. Williams Python 5 08-17-2007 09:32 PM
sub-classing the types in the builtin module datetime Colin J. Williams Python 1 08-15-2007 05:03 PM
Pointer to operator for builtin types =?iso-8859-1?q?Erik_Wikstr=F6m?= C++ 6 12-15-2006 11:39 AM
Overriding builtin types Lonnie Princehouse Python 0 03-05-2004 07:53 PM



Advertisments