Assignment and comparison in one statement

Jarek Zgoda jzgoda at o2.usun.pl
Sat May 24 08:18:13 EDT 2008


Johannes Bauer pisze:

> I'm just starting with Python and am extremely unexperienced with it so
> far. Having a strong C/C++ background, I wish to do something like
> 
> if (q = getchar()) {
>     printf("%d\n", q);
> }
> 
> or translated to Python:
> 
> if (p = myfunction()):
>     print p
> 
> However, this "assignment and comparison" is not working. What's the
> "Python way" of doing this kind of thing?

Assignment operation (or, as we prefer calling: "binding value to name")
has no boolean value, because it succeeds always. While call to
myfunction can return any value, binding it to name p has no value at all.

Tell us your objective, so we could show you "proper way" (tm). ;)

-- 
Jarek Zgoda
http://zgodowie.org/

"We read Knuth so you don't have to" - Tim Peters



More information about the Python-list mailing list