My newbie annoyances so far

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sat Apr 28 05:46:44 EDT 2007


On Sat, 28 Apr 2007 10:58:25 +0200, Bjoern Schliessmann wrote:

> Dennis Lee Bieber wrote:
> 
>> You didn't take account of what b, c, and d were...
>> 
>> RPL:          <condition> if <truth> else <false> end
>> Python:               <truth> if <condition> else <false>
>> 
>> (RPL is a somewhat common reference to the stack based language of
>> the later calculators -- HP48, for instance)
> 
> I still don't see the "more sense". Python's variant seems logical
> to me -- "<fetch out garbage> if <garbage can full> else <don't>".
> 
> The HP equivalent will be, if I understand correctly:
> 
> "<garbage can full> if <fetch out garbage> [else <don't>]"

As I explained in another post, the syntax given is wrong for RPL, but if
it were right, then that description would be correct: the if would pop a
flag off the stack, in this case <garbage can full>, and then branch to
either the code after the if <fetch out garbage> or after the else <don't>.


> I see two problems here:
> 
> - Also from my error in the last posting it's quite clear that the
> RPL statement doesn't do what one would suppose.

There are bad programmers in every language, but RPL conditional blocks
aren't the cause of them. Once you learn how RPL works, if statements work
consistently and obviously (although maybe not to programmers who don't
get RP notation).


> Isn't this what
> Python always tries to avoid: Doing something different from what
> is "obvious". RP order doesn't fit in Python, IMHO.

I agree that blindly trying to copy RP notation into Python wouldn't work.


> - What should the expression's value be if the else is omitted?
> None? What's it in the original? I can't imagine a use case here
> since I always have two alternatives when I use "a if b else c".

In RPL, there are no expressions. RPL programs are constructed from data
and commands, not expressions. So you shouldn't think of 

<garbage can full> if <fetch out garbage> [else <don't>]

as an expression. Think of it as a block, equivalent to the Python:

if garbage_can_full:
    fetch 
    out
    garbage
else:
    don't

except that you can write it as a single line. Newlines in RPL are just
another sort of whitespace, with no special significance.

If the else clause is missing, then nothing is executed and processing
simply continues past the end of the block.

I agree with you that in the case of Python _expressions_, it doesn't make
sense to have a missing else part. But in the case of RPL, it is perfectly
natural for the else block to be missing.


-- 
Steven.




More information about the Python-list mailing list