deriving from str

dataangel k04jg02 at kzoo.edu
Mon Dec 27 09:01:45 EST 2004


Paolo Veronelli wrote:

> I want to add some methods to str class ,but when I change the 
> __init__ methods I break into problems
>
> class Uri(str):
>     def __init__(self,*inputs):
>         print inputs
>         if len(inputs)>1:
>             str.__init__(self,'<%s:%s>'%inputs[:2])
>         else:
>             str.__init__(self,inputs[0])
>         print inputs
> a=Uri('ciao','gracco')
>
> Traceback (most recent call last):
>   File "prova.py", line 9, in ?
>     a=Uri('ciao','gracco')
> TypeError: str() takes at most 1 argument (2 given)
>
>
> where is the  str() wrong call.I suppose It's the __new__ method which 
> is wrong or me .Thanks for help
>
I think the problem is that you're not callinig the init method for str 
by hand, so it's caling it automatically and passing to it the same 
paramters you gave Uri's init. str just expects self to be passed, not 
also *inputs, so you get that error.



More information about the Python-list mailing list