a list or not a list?

Alex Martelli aleax at aleax.it
Wed May 7 10:39:51 EDT 2003


Axel Grune wrote:

> Alex Martelli schrieb:
>> To be precise: *the arguments which print_exc passes to the write method
>> of the filelike object it's given* most definitely AREN'T list or tuples.
>> They're strings, of course.
> 
> actually I expected a string 

So far so good.

> and I thought that the write method is
> executed only _once_. Since there were not one string but four, I

Ah, that's the rub -- there's no particular reason to expect that,
as I tried pointing out.  If you start with a list of strings, the
fastest approach (as well as the most convenient one) is probably
to call the writelines method once with that list as its argument.

But not all file-like objects will have writelines (yours didn't,
for example:-) -- and, if you're going to loop anyway, it's generally
more convenient to call write at each step (once _or more_) rather
than accumulating a list of strings and calling writelines at the
end -- it may even be faster (as it saves memory -- and the file-
like object can buffer things to its heart's content, if that will
help performance...).

> thought it could be some kind of list. That's why I talked about lists
> and tuples. I thought a little bit about it and looked into the
> traceback.py module and found out that the write method is called for
> every line of the traceback. Since this traceback had four lines, I got
> four strings. I know, I was a little bit unaccurate what my actual
> problem was.

Yes, but I don't even see that it's a problem:-).


Alex





More information about the Python-list mailing list