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

Pearu Peterson pearu at cens.ioc.ee
Mon May 13 18:11:30 EDT 2002


On 13 May 2002, 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 ...

You can do

  a = b(c)
  if a is not None:
    use a here ..


Regards,
	Pearu






More information about the Python-list mailing list