subclassing builtin types
aleaxit at yahoo.com
Fri Oct 8 09:39:29 CEST 2004
BJörn Lindqvist <bjourne at gmail.com> 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):
> 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.
More information about the Python-list