Re: Frankenstring
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Frankenstring

From: Scott David Daniels <Scott.Daniels@Acm.Org>
Date: Tue Jul 12 2005 - 23:38:03 CEST

Thomas Lotze wrote:
> Hi,
> I think I need an iterator over a string of characters pulling them out
> one by one, like a usual iterator over a str does. At the same time the
> thing should allow seeking and telling like a file-like object:
>
>
>>>>f = frankenstring("0123456789")
>>>>for c in f:
>
> ... print c
> ... if c == "2":
> ... break
> ...
OK, frankenstring can be approximated by nothing:
for c in "0123456789":
     print c

Now if you want to do it for a file, you could do:

     for c in thefile.read():
         ....

Or, if you did not want to do a full read:
     def filechars(afile):
         for line in afile:
             for char in line:
                 yield char

--Scott David Daniels
Scott.Daniels@Acm.Org
Received on Thu Sep 29 16:54:58 2005