subclassing Python types

Steve Holden steve at holdenweb.com
Thu Aug 30 23:12:58 CEST 2007


Arnaud Delobelle wrote:
> On Aug 30, 8:44 pm, Robert Kern <robert.k... at gmail.com> wrote:
>> zzbba... at aol.com wrote:
>>> So it's:
>>> class MyString(str):
>>>    def __init__(self,strInput):
>>>       self = strInput
>> That doesn't quite work. Assigning to "self" only reassigns the name inside the
>> function. It does not replace the object.
>>
>> Instead, call the .__init__() method on str.
> 
> That won't do much as strings are immutable objects...
> As pointed out by Steve Holden and me, str.__new__ is the way.

I suspect the OP is analogous to the drunk who was looking for his key 
next to a lamp post. When a helper became exasperated with the lack of 
results he started to question the drunk about where he'd dropped the 
key. "Over there" says the drunk, pointing about twenty yards away. 
"Then why are you looking over here?". Comes the reply:
  "Because you can't see a bloody thing where I dropped it."

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list