Assignment and comparison in one statement
Carl Banks
pavlovevidence at gmail.com
Fri May 23 21:00:29 EDT 2008
On May 23, 6:59 pm, Johannes Bauer <dfnsonfsdu... at gmx.de> wrote:
> Hello group,
>
> 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?
p = myfunction()
if p:
print p
(I recommend doing it this way in C, too.)
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.
Carl Banks
More information about the Python-list
mailing list