new method for string objects

Fredrik Lundh effbot at telia.com
Sat Apr 1 09:23:25 EST 2000


Andrew Dalke <dalke at acm.org> wrote:
>   I know with 1.6 strings will have methods.  If it isn't
> already there, I would like to propose an additional method,
> "tostring()", which returns itself.
>
>   I'm working on a project which may use array.array of
> characters, Numeric.array, or a string, and I would like
> to have a way that's guaranteed to return the string
> value for the object.

assuming that "string value" means the "raw contents of the
sequence object", you should use a buffer objects instead.
you can use the 'buffer' builtin to turn most sequence objects
into buffers:

>>> s = u"123"
>>> b = buffer(s)
>>> b
<read-only buffer for 7969b0, ptr 7969d0, size 6 at 796530>
>>> len(b)
6
>>> b[0]
'1'
>>> str(b)
'1\0002\0003\000'
>>> fp.write(b) # writes raw contents to file without extra copy

(if numeric python doesn't support the buffer interface,
it should be fixed).

</F>





More information about the Python-list mailing list