New to Python - block grouping (spaces)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Apr 16 13:10:01 EDT 2015


On Thu, 16 Apr 2015 08:51 pm, BartC wrote:

> On 16/04/2015 06:49, Steven D'Aprano wrote:
>> On Thursday 16 April 2015 14:07, Blake McBride wrote:
> 
>>> Is there a utility that will allow me to write Python-like code that
>>> includes some block delimiter that I can see, that converts the code
>>> into
>>> runnable Python code?  If so, where can I find it?
> 
>> No more bugs from accidentally forgetting to use optional braces!
> 
> You get bugs instead from mistakenly using the wrong indentation or
> losing the correct indentation (accidentally pressing the wrong key for
> example).

That's nothing! Python gives you absolutely no protection from accidentally
typing:


    x += 1

when you actually wanted:

   y -= 2


And there there was the time I edited some code written by my boss. I
intended to write a comment:

    # FIXME: this function is a little slow and should be optimized.

but I hit the wrong key a couple of times and wrote:

    # This is a steaming pile of guano written by a drooling
    # moron who shouldn't be allowed near a computer. It needs 
    # to be deleted with prejudice, and the hard drive and all 
    # backup tapes set on fire just to be sure.


As I'm sure you will agree, it is an easy mistake to make.


I'm not impressed by arguments "braces protect you from accidentally hitting
tab too many times (or too few)". Um, okay. Don't people read their own
code? How do you not notice that you've indented it wrongly?

To my mind, the argument from "hitting tab too many times" is like tying a
double-bed mattress to your head in case an eagle flying overhead drops a
tortoise on your head.


The great thing about Off-side rule languages like Python is that the
structure of code is easy to see:


code code code code code code code 
    code 
code code code 
    code code code 
        code code code 
    code 
        code
            code
                code code code 
                    code code
                code code 
                    code code
    code
code


while braces without indentation are horribly opaque:

code code code code code code code { code } code code code { code 
code code { code code code } code { code { code { code code code
{ code code } code code { code code } } } } code } code



It's true that if a tool mangles your source code and deletes all your
indentation, you cannot mechanically fix the problem without understanding
the intent of the source code. But if a tool mangles your source code by
deleting words starting with "w", the same applies. Don't use broken tools.



-- 
Steven




More information about the Python-list mailing list