while c = f.read(1)

Paul Rubin http
Mon Aug 22 11:48:24 CEST 2005

"Greg McIntyre" <greg at puyo.cjb.net> writes:
>   while c = f.read(1):
>       # ...
> I couldn't find any general PEPs along these lines, only specific ones
> (e.g. 308 re. an if-then-else expression).

I often end up doing something like this:

   class foo:
      def set(self, x):
          self.x = x
          return x
   c = foo()

   while c.set(f.read(1)):
     # do stuff with c.x

In that file example, it's too much nuisance, but when you're
comparing some input against a series of regexps and you'd otherwise
need a multi-line construction for each one, this method comes in
quite handy.

More information about the Python-list mailing list