On 10/14/10 12:53, Paul Rubin wrote:
Carl Banks<pavlovevide...@gmail.com>  writes:
In general, the only way to test if a generator is empty is to try to
consume an item.  (It's possible to write an iterator that consumes an
item and caches it to be returned on the next next(), and whose
boolean status indicates if there's an item left. ...)

I remember thinking that Python would be better off if all generators
automatically cached an item, so you could test for emptiness, look
ahead at the next item without consuming it, etc.  This might have been
a good change to make in Python 3.0 (it would have broken compatibility
with 2.x) but it's too late now.

Generators can do dangerous things...I'm not sure I'd *want* to have Python implicitly cache generators without an explicit wrapper to request it:

 import os
 from fnmatch import fnmatch

 def delete_info(root, pattern):
   for path, dirs, files in os.walk(root):
     for fname in files:
       if fnmatch(fname, pattern):
         full_path = os.path.join(path, fname)
         info = gather_info(full_path)
         os.unlink(full_path)
         yield full_path, info

 location = '/'
 user_globspec = '*.*'
 deleter = delete_info(location, user_globspec)
 if some_user_condition_determined_after_generator_creation:
   for path, info in deleter:
     report(path, info)

-tkc
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to