str class inheritance prob?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Apr 18 17:39:34 CEST 2008


jkazoo at gmail.com a écrit :
> 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:

Others already gave you the technical solution (use __new__, not 
__init__). A couple remarks still:


1/

> 
> 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

Assigning to self - or to any other local name - only impact the local 
namespace, it won't affect the object previously bound to that name.

2/ you may be interested in the existing path module:
http://www.jorendorff.com/articles/python/path/

HTH



More information about the Python-list mailing list