StringIO objects sharing a buffer

T

Thomas Lotze

Hi,

I want to implement a tokenizer for some syntax. So I thought I'd subclass
StringIO and make my new class return tokens on next().

However, if I want to read tokens from two places in the string in turns,
I'd either need to do some housekeeping of file pointers outside the
tokenizer class (which is ugly) or use two tokenizers on the same data
buffer (which seems impossible to me using my preferred approach as a
file-like object has exactly one file pointer).

Is there a way for multiple StringIO objects to share a buffer of data, or
do I have to give up on subclassing StringIO for this purpose? (An
alternative would be a tokenizer class that has a StringIO instead of
being one and do the file pointer housekeeping in there.)
 
M

Mathias Waack

Thomas said:
Is there a way for multiple StringIO objects to share a buffer of
data, or do I have to give up on subclassing StringIO for this
purpose? (An alternative would be a tokenizer class that has a
StringIO instead of being one and do the file pointer housekeeping
in there.)

I'm not sure if I understand the problem right, but there is a rule
of thumb:
Prefer delegation to inheritance. If you can use delegation, avoid
inheritance. Esp. on python, where (from the point of view of usage)
there is no distinction between delegation or inheritance.

Mathias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top