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

Daniel T. notdanielt3 at gte.net
Mon May 13 18:05:42 EDT 2002


In article <12257ec4.0205131401.25c63801 at posting.google.com>,
 davidccarson at hotmail.com (David Carson) wrote:

>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

How about:

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

Or if you don't like having 'a' hang around after the if block:

def doThing( a ):
   if a:
      #use a here ...

doThing( b(c) )

-- 
Improve your company's understanding of objects...
Hire me. <http://home1.gte.net/danielt3/resume.html>



More information about the Python-list mailing list