In <[EMAIL PROTECTED]>, Basilisk96
wrote:

> "Terry Reedy" <[EMAIL PROTECTED]> wrote:
>> Any what if 'filelist' is any iterable other than a string or list?  Your
>> code is broken, and unnecessarily so.  So I would call the parameter
>> 'files' and test for isinstance(files, str) #or basestring.  And wrap if it
>> is.
> 
> Can you give an example of such an iterable (other than a tuple)? I'd
> certainly like to fix my 'fix' to work for a more general case.

def iter_filenames(filename):
    lines = open(filename, 'r')
    for line in lines:
        yield line.rstrip()
    lines.close()

filenames = iter_filenames('files.txt')

Now `filenames` is an iterable over strings representing file names but
it's not a `list`.  And it's easy to come up with iterables over strings
that produce the data themselves, for example by attaching a counter to a
basename, or extracting the names from XML files, fetching them from a
database etc.

Ciao,
        Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to