Assignment and comparison in one statement

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat May 24 07:35:49 EDT 2008


On Sat, 24 May 2008 13:12:13 +0200, Johannes Bauer wrote:

> Carl Banks schrieb:
> 
>> p = myfunction()
>> if p:
>>     print p
>> 
>> (I recommend doing it this way in C, too.)
> 
> This is okay for if-clauses, but sucks for while-loops:
> 
> while (fgets(buf, sizeof(buf), f)) {
> 	printf("%s\n", buf);
> }
> 
> is much shorter than
> 
> char *tmp;
> tmp = fgets(buf, sizeof(buf), f);
> while (tmp) {
> 	printf("%s\n", buf);
> 	tmp = fgets(buf, sizeof(buf), f);
> }

Can be written in Python as:

from functools import partial

# ...

for buf in iter(partial(f.read, buf_size), ''):
    print '%s\n' % buf

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list