+= overloading?

Neil Macneale mac4-devnull at theory.org
Mon Jun 18 13:13:41 EDT 2001


In article <mailman.992755945.11129.python-list at python.org>, "Roman Suzi"
<rnd at onego.ru> wrote:

> On Sat, 16 Jun 2001, Neil Macneale wrote:
> 
>>I am creating a colored stringbuffer class, and I want to append words
>>to the buffer with the '+=' operator.  Ex:
>>
>>>>>csb = CSB("INIT STRING")
>>>>>csb += "Hello"
>>>>>print csb
>>INIT STRINGHello
>>
>>would be the desired effect.  I can't seem to find anything in the lib
>>docs about overloading that operator though.  Am I looking in the wrong
>>place?  It is also important that a new object is NOT created every time
>>the operator is used, because that would be a waste.
> 
> 
> class CSB:
>   def __iadd__(self, x):
>     self._string += x
> 

I am using a list as the data holding element, then appending to it in
the __iadd__ function. Then I use ''.join(list) to make a string
when the __str__ function is called.  I am doing this to minimize the
number of string objects created,  and figure that append runs in
constant time.  Is join optomized though?  It would defeat the purpose if
join worked by creating a new string to join the first two, then added
the third, etc.  I am assumming that join is implemented in C, and runs
in 0(n) time, where n is the number of total characters.  Am I assuming
incorrectly?  I looked at string.py's join function, but that just called
another join method, and I don't know where that one is implemented.

Thanks,  Neil Macneale

--
To reply to me via email, remove the '-devnull' from my address.



More information about the Python-list mailing list