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

David Carson davidccarson at hotmail.com
Tue May 14 15:18:49 EDT 2002


I figured I'd get a lot of answers if I put it that way!  

In my own defense:
I love Python and I have no desire to see Python look like C.  This is
one particular instance only where I would have liked the C construct.

Also, note that I was not looking for this:
  if (a = b(c))
but rather
  if ((a = b(c)) != NULL)

I would never do the former, even in C, although that is the 'classic'
way of coding such a statement.

As I've had all the answers I need now, I'll continue to do it as
everyone has suggested.

Thanks all,
David

"Steve Holden" <sholden at holdenweb.com> wrote in message news:<k99E8.4251$Wh.3930 at atlpnn01.usenetserver.com>...
> "David Carson" <davidccarson at hotmail.com> wrote in message
> news:12257ec4.0205140344.2680b226 at posting.google.com...
> > "Mark McEahern" <marklists at mceahern.com> wrote in message
>  news:<mailman.1021328064.29854.python-list at python.org>...
> > > [David Carson]
> > > > 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 ...
> > >
> > > a = b(c)
> > > if a:
> > >   ...
> > >
> > > // m
> >
> >
> > OK, so I wasn't quite clear in my original post.  I got four fine
> > answers like this one -- that is how I do it too.
> >
> So what you really want to know is, why isn't Python more like C?
> 
> > They lack the other criterion that makes me use this style in C in the
> > first place -- brevity.  I choose the C syntax in the header because I
> > can save a line _and_ it is as clear and readable as the alternative
> > of doing it on two lines (as all these solutions suggest).  I would
> > not choose it if it was not readable.
> >
> I would suggest that readability is a matter of opinion. Further, the
> original C consgtruct *is* a source of errors (use of "=" where "==" should
> have been used).
> 
> > So, I'll ask again: How do I do the equivalent in Python so that it is
> > both clear and concise?
> >
> You don't, because concise is less clear. See
> 
>     http://www.python.org/cgi-bin/faqw.py?req=show&file=faq06.030.htp
> 
> for a rationale behind the specific decision to exclude that particular
> conciseness.
> 
> readability-counts-ly y'rs  - steve



More information about the Python-list mailing list