python for this C: "if ((a = b(c)) != NULL)"

David Carson davidccarson at hotmail.com
Mon May 13 18:01:23 EDT 2002


This feels like a stupid question, but I'm having trouble seeing
outside of my C background right now.

I expected the C syntax above to work in Python, with None replacing
NULL, but it complains about the assignment in the inner parentheses. 
In C, of course, the inner assignment has a side effect ('a' gets the
value) and has a value that can be compared to NULL.

So, how do I do this in Python, since I want to avoid running method
b() twice in the case where I want to use 'a' later?  In other words,
I don't want to do:

  if b(c):
    a = b(c)
    use a here ...

David



More information about the Python-list mailing list