str class inheritance prob?

s0suk3 at gmail.com s0suk3 at gmail.com
Wed Apr 16 16:35:09 EDT 2008


On Apr 16, 1:43 pm, jka... at gmail.com wrote:
> so I’m trying to create a class that inherits from str, but I want to
> run some code on the value on object init.  this is what I have:
>
> class Path(str):
>             def __init__( self, path ):
>                         clean = str(path).replace('\\','/')
>                         while clean.find('//') != -1:
>                                     clean = clean.replace('//','/')
>
>                         print 'cleaned on init:\t',clean
>                         self = clean
>
> so clearly the clean variable is what I want value of the string to
> be, but that’s decidedly not the case.  so running this:
>
> a=Path('path///with\\nasty/////crap_in_it/')
> print a
>
> gives me this:
>
> cleaned on init:  path/with/nasty/crap_in_it/
> path///with\nasty/////crap_in_it/
>
> what gives?  what am I doing wrong, and can I do what I’m trying to
> here?
> thanks.

Regardless of the problem you have initializing the class, why do you
need to inherit from str? Actually, why do you even need a class?
Unless you're dealing something more complex that you didn't mention
here, that should be just a simple function.



More information about the Python-list mailing list