what is wrong with this line? while (line = workfile.readline()) != "":

D-Man dsh8290 at rit.edu
Tue May 22 16:50:53 EDT 2001


On Tue, May 22, 2001 at 02:07:44PM -0600, erudite wrote:
| Thank you all for your responses. Question though. Is there some sort of
| advantage to not allowing statements where a expressions is expected?

Try this (C):

int x = 0 ;

if ( x = 0 ) 
{
    printf( "x is 0!" ) ;
}
else
{
    printf( "x is NOT 0" ) ;
}


What will be printed?

As a result of assignment being an expression, and thus allowed here,
and no actual boolean type (an integer is used instead) and the
lexical similarity of '=' to '==' (some people feel) it is too easy to
make such a typo.  If you were to try this in python, the interpreter
would scream and yell at you to alert you of the problem that needs
fixing.

I think the usefulness of assignment in a condition is mostly limited
to input situations like the one you presented.  Python has an idiom
for such situations that IMO works just fine and is prettier than the
equivalent C/C++/Java idiom.

There may be some historical basis in the way statements vs.
expressions are imlemented, but I don't know of any.

-D





More information about the Python-list mailing list