while c = f.read(1)

Bengt Richter bokr at oz.net
Tue Aug 23 19:34:14 CEST 2005

On 22 Aug 2005 02:48:24 -0700, Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

>"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.

if you use def __call__ instead of def set, you can also simplify the
subsequent spelling e.g,

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

though something more menmonic than c might be desirable in that case.

Bengt Richter

More information about the Python-list mailing list