subclassing str

not1xor1 (Alessandro) " " at libero.it
Mon Nov 8 05:42:41 CET 2010


Il 07/11/2010 07:41, Chris Rebert wrote:

> You could subclass UserString instead of str; all of UserString's
> methods seem to ensure that instances of the subclass rather than just
> plain strs or UserStrings are returned. See
> http://docs.python.org/library/userdict.html#UserString.UserString

I'll have a look at it, thanks

> But you should also consider whether your additions absolutely *must*
> be methods. Merely instead defining some functions that take strings
> as parameters is obviously a simpler, and probably more performant,
> approach.

I regularly save web pages (mostly scientific research abstracts) from 
various web sites and use a python script to strip them of ads and 
unneeded informations, embedding the images directly in the html file 
(as base64 encoded data) and at times joining multiple pages into just one

since those sites often change the format of their files I've to 
modify my script accordingly

I'm already using plain functions, but thought that wrapping most of 
them in a str subclass would let me save some time and yield cleaner 
and more manageable code

> If you insist on subclassing str, there's no such hook; you'll have to
> override all the methods yourself.*

I'll try this route too (at least for the methods I need)
thanks for your help
-- 
bye
!(!1|1)



More information about the Python-list mailing list