[Tutor] should this cause a syntax error?

Magnus Lycka magnus@thinkware.se
Sat Nov 2 12:24:01 2002


At 05:52 2002-11-03 +1300, Rob Brown-Bayliss wrote:
>self.some_value == FALSE

>Shouldn't the first one have caused a syntax error as it was not in an
>if or while loop etc?

No, you are allowed to put expressions in your code,
even if you don't take care of the return value. It's
common that you don't care about the results of
expressions. Not exactly _that_ expression maybe, but
in other cases.

Although, if self.some_value is an instance of a class
that has implemented __eq__ or __cmp__ you might want to
use a side effect but not care about return values in this
case.

Do you think it should be illegal to write:

my_function(x, y)

instead of

return_value_I_dont_care_about = my_function(x, y)

or should this be illegal:

f1(x,y) or f2(x,y,z)

The latter uses the lazy evaluation in Python to make
sure that f2 is only called if f1 returns a value which
is equivalent of false.

But you might have intended:

x = f1(x,y) or f2(x,y,z)

Where should Python draw the line?

In the Computer Programming For Everybody (CP4E) project,
Guido had planned different language levels in Python,
where syntax which *could* be intentional but was probably
a mistake would give syntax error. I don't know if your
case would be an example of that.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se