Assignment and comparison in one statement

Johannes Bauer dfnsonfsduifb at gmx.de
Sat May 24 07:12:13 EDT 2008


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);
}

> For the case where you want to do this in an elif-clause, look for the
> recent thread "C-like assignment expression?" which list some
> workarounds.  Or just Google this newsgroup for "assignment
> expression".  It's one of few minor irritating things in Python
> syntax.

Alright, I will. Thanks.

Regards,
Johannes

-- 
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen."     -     Wolfgang Gerber
       in de.sci.electronics <47fa8447$0$11545$9b622d9e at news.freenet.de>



More information about the Python-list mailing list