Why not allow empty code blocks?

BartC bc at freeuk.com
Sun Jul 24 09:09:29 EDT 2016


On 24/07/2016 11:45, BartC wrote:
> On 24/07/2016 11:35, BartC wrote:

> 'end' to terminate a block can be emulated of course:
>
> end=0
>
> def fn(a):
>     if a<=1:
>     return 1
>     else:
>         return fn(a-1)
>     end
> end

Actually this is a good example of how tabs can go wrong (and how the 
tab system /is/ fragile - sorry but it is).

I almost certainly wrote the above using 4 and 8 spaces for the tabs, 
except for the 'return 1' where I must have used an actual tab by 
mistake. (And I tested it now by doing just that, and posting in alt.test.)

So the original /looked/ correct in my Thunderbird newsreader before I 
posted. But after I posted, that tab somehow got changed to 4 spaces, as 
it now looks wrong.

In this instance, the result won't compile. But it's not hard to imagine 
a much larger program where that change would go unnoticed, and the 
result is still valid code**.

Then anyone copying and pasting the posted code, would have a program 
with a bug in it.

Mysteriously however, Chris Angelico's reply which quoted my post, 
showed a properly tabbed version! (Unless he fixed it manually.)

(** Where working code has been posted, then Python will have picked up 
inconsistencies where tabs and spaces are mixed. However take this code:

def fn():
<tab>if a:
<8 spaces>pass

This looks fine in my editor when <tab> is expanded to 4 spaces:

def fn():
     if a:
         pass

Python however doesn't like it (Python 2 doesn't anyway), because it 
somehow assumes tabs expand to 8 spaces, so that the two indents look 
like this to it:

def fn():
         if a:
         pass

So I can see a lot of problems whenever tabs are expanded differently:

a=1
b=0

if a:
<tab>if b:
<tab><tab>print ("One")
<8 spaces>print ("Two")

In my editor with 4-space tabs, it looks like the code will print 
nothing as the two print lines are aligned within the 'if b:' block. But 
in Python 2, it will print "Two". Python 3 more wisely reports the 
inconsistency.)

-- 
Bartc





More information about the Python-list mailing list