Code design for a sub-class of built-ins

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Jul 5 08:48:15 EDT 2006


On Wed, 05 Jul 2006 11:41:47 +0200, Bruno Desthuilliers wrote:

> Steven D'Aprano wrote:
>> On Tue, 04 Jul 2006 19:26:36 +0200, Bruno Desthuilliers wrote:
>> 
>> 
>>>Steven D'Aprano wrote:
>>>
>>>>I'm having problems with sub-classes of built-in types.
>>>>
>>>>Here is a contrived example of my subclass. It isn't supposed
>>>>to be practical, useful code, but it illustrates my problem.

[snip]

> For the record, care to give more informations about your real use case?

Equal parts a learning exercise and a simple example of a genetic
algorithm.

I felt that the natural way to approach this would be for an object to
mutate itself, rather than have an external function that operated on a
string and returned a new string. The obvious approach was to subclass str
and give it methods to modify itself in place, but of course strings are
immutable.

This got me wondering how hard it would be to create a mutable string
class, whether I should look at subclassing list (to get the mutability)
and then add string-like methods to it, or try something completely
different.

A little bit of experimentation soon had me realising that I didn't
understand Python's new-style class model well enough to do these things
properly, hence the learning exercise.


Thanks for everybody's help on this.



-- 
Steven.




More information about the Python-list mailing list