29

I'm looking into overloading the + operator for a certain string so I was thinking of subclassing the string class then adding the code in the new class. However I wanted to take a look at the standard string class first but I can't seem to find it... stupid eh?

Can anyone point the way? Even online documentation of the source code.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
momo
  • 1,045
  • 3
  • 9
  • 18
  • It may be C Extention, and builtin type so I think its not possible to open the code as .py file what you think... – shahjapan Sep 29 '10 at 10:06
  • "I'm looking into overloading the '+' operator for a certain string". "overloading" for "a certain string" isn't anything sensible under any circumstances. You probably want a new class of objects entirely that play well with strings. You should provide more information, since what you're describing doesn't make much sense. – S.Lott Sep 29 '10 at 11:45
  • well the specific case is overloading the string so that when i add use the __add__ method with a numeric type (int, float, etc), it will 'increment' to the corresponding letter. e.g. 'A' + 2 = 'C'. however since i've never done any type of overloading before, i'm also interested in the way operators are overloaded in the general sense, not just in this particular situation. – momo Sep 29 '10 at 14:43
  • "it will 'increment' to the corresponding letter. e.g. 'A' + 2 = 'C'."? That's not a "string". That's a "character". A highly-resticted and specialized class of objects that happens to provide a string result when used with other strings? Is that what you're talking about? – S.Lott Sep 29 '10 at 16:52

4 Answers4

16

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 1
    Link is dead now. See https://svn.python.org/projects/python/trunk/Objects/stringobject.c – Tan Wang Sep 04 '17 at 22:13
  • 4
    And... Link is dead again. I guess the source code is now located on Github, in [`Objects/unicodeobject.c`](https://github.com/python/cpython/blob/master/Objects/unicodeobject.c). – Delgan Nov 14 '17 at 21:01
2

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'
S.Lott
  • 384,516
  • 81
  • 508
  • 779
0

my implementation is similar, though not as neat:

class MyClass:
    def __init__(self, name):
        self.name = name
    def __add__(self, other):
        for x in range(1, len(self.name)):
            a = list(self.name)[-1 * x]
            return self.name[:len(self.name)-x] + chr(ord(a) + other)


>>> CAA = MyClass('CAA')
>>> CAA + 1
'CAB'
>>> CAA + 2
'CAC'
>>> CAA + 15
'CAP'
momo
  • 1,045
  • 3
  • 9
  • 18
  • 1
    "not as neat"? It's got weird, weird problems. Why is there a `return` in the middle of of the `for` loop? That will perform the "loop" exactly once and stop. There's no actual "loop" going on. This can be simplified considerably. – S.Lott Sep 29 '10 at 21:50
  • Also, unless you're using Python 3, please make it a subclass of `object`. – S.Lott Sep 29 '10 at 21:51
0

This appears to be a link that is not currently dead in 2020: https://svn.python.org/projects/python/trunk/Objects/stringobject.c

Justin Furuness
  • 685
  • 8
  • 21