Location of python string class in the source code
You might mean this.
class MyCharacter( object ):
def __init__( self, aString ):
self.value= ord(aString[0])
def __add__( self, other ):
return MyCharacter( chr(self.value + other) )
def __str__( self ):
return chr( self.value )
It works like this.
>>> c= MyCharacter( "ABC" )
>>> str(c+2)
'C'
>>> str(c+3)
'D'
>>> c= c+1
>>> str(c)
'B'
It's documented here. The main implementation is in Objects/stringobject.c
. Subclassing str
probably isn't what you want, though. I would tend to prefer composition here; have an object with a string field, and special behavior.