How coding in Python is bad for you

BartC bc at freeuk.com
Tue Jan 24 07:12:05 EST 2017


On 24/01/2017 04:22, Steven D'Aprano wrote:
> On Tuesday 24 January 2017 13:38, Chris Angelico wrote:
>> On Tue, Jan 24, 2017 at 12:47 PM, BartC <bc at freeuk.com> wrote:

>>> if 0 then
>>>     print ("one")
>>> print ("two")
>>> endif

>> My point is that you *assume* that showing just "three" is the correct
>> behaviour. Why? Why do you automatically assume that the indentation
>> is wrong and the endif is correct? All you have is that the two
>> disagree.
>
> It's Bart's special language, so the correct behaviour is whatever he says it
> is :-)

I chose 'if...endif' above because it's used in a number of well-known 
languages. (Including PHP I think, while others use 'end if' or just 
'end'. In my own language I prefer 'if...fi')


> This *especially* applies to languages like C, when open/close delimiters
> optional if the block is only a single statement, and where the delimiters are
> only a single character.
>
> And sure enough, C is prone to indent/brace mismatch errors.

I didn't use brace syntax in the example for that reason. C has plenty 
of its own problems. Although funnily enough, its preprocessor language 
uses '#if...#endif'; much more sensible!

C suffers also from the 'dangling else' problem, which a 'if...endif' 
syntax doesn't.

Nor does Python's scheme, as the 'else' /has/ to be aligned with the 
corresponding 'if' or 'elif'. However... take this code:

   if a:
       b
       if c:
           d

You want to import an 'else' block from elsewhere to add to that nested if:

   if a:
       b
       if c:
           d
   else:
       e

The trouble is, that other code was not nested; it will need extra 
indentation here. Until that happens however, the above is still legal.

-- 
Bartc



More information about the Python-list mailing list