How coding in Python is bad for you

BartC bc at freeuk.com
Mon Jan 23 20:47:07 EST 2017


On 24/01/2017 00:56, Chris Angelico wrote:
> On Tue, Jan 24, 2017 at 11:44 AM, BartC <bc at freeuk.com> wrote:

>> With C++ or Java, it's possible to tell the indentation is wrong (because of
>> the extra redundancy of having the indentation /and/ block delimiters).
>> That's a bit harder in Python making source more fragile.
>
> With C++ or Java, it's possible to tell that the braces are misplaced
> (because of the extra redundancy). When that happens, the compiler
> won't tell you; it'll just silently do the wrong thing. In Python,
> that can't happen. Python is therefore more robust.
>
> What's to say which is correct?

I don't get you. Start with this code

if 0:
     print ("one")
     print ("two")
print ("three")

It prints "three". Now the second indent accidentally gets deleted:

if 0:
     print ("one")
print ("two")
print ("three")

It still runs, but now prints "two", "three". Take the same code with 
block delimiters, and take out that same indent:

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

It still compiles, it still runs, and still shows the correct "three" as 
output. And the discrepancy can be caught by extra tools (that you say 
ought to be used), or it is obvious visually anyway that the "two" line 
belongs with the "one" line not the "three" line.

Yes, delimiters can be placed wrongly, but that is a harder error to 
make just by pressing one key (deleting a tab, or pressing Tab).

(Someone is now going to tell me that it is possible to declare:

   endif = 0

in Python, and use 'endif' at the end of a block just like my example. 
Now any adjacent line one tab out of place will be equally obvious. 
However, no one does that! The language needs to require it and enforce it.)

>> but as it's normally used,
>> many errors are not detected until execution.

> Plenty of editors can catch those even before you run the code.

Extra tools...

(But how would it catch assigning to a wrongly spelled attribute?)

-- 
Bartc



More information about the Python-list mailing list