New to Python - block grouping (spaces)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Apr 16 07:49:25 CEST 2015


On Thursday 16 April 2015 14:07, Blake McBride wrote:

> Greetings,
> 
> I am new to Python.  I am sorry for beating what is probably a dead horse
> but I checked the net and couldn't find the answer to my question.
> 
> I like a lot of what I've seen in Python, however, after 35 years and
> probably a dozen languages under my belt, I very strongly disagree with
> the notion of using white space to delimit blocks.  Not wanting to beat
> what I believe is probably a dead horse, I have one question.
> 
> 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?

Python already supports this with the optional "block delimiter" sigil. For 
example, if you like braces, you can write:


def function(x):
#{
    if x > 1:
        #{
        print "x bigger than 1"
        #}
    else:
    #{
        print "x smaller than or equal to 1"
        while x <= 1:  #{
            x += 1  #}
        #}
    return x
#}

Notice that Python is clever enough to allow many brace styles, or even 
allow you to invent your own style.

If you prefer Pascal style, Python supports that too:

def function(x):
    # BEGIN
    return x
    # END

You can even localise it:

def function(x):  # ANFANGEN
    return x
    # BEENDEN


Best of all, Python's parser includes an advanced "Do What I Mean" expert 
system which can infer the intended block grouping from the indentation 
alone, even when braces are missing or in the wrong position. Unlike C, 
Python will do the right thing here:


if condition:
    do_this()
    do_that()


No more bugs from accidentally forgetting to use optional braces!



Sorry for the flippant response, but it's 2015, not 1995, and the question 
about the Offside Rule is not just a dead horse but it is positively 
fossilized.

https://en.wikipedia.org/wiki/Off-side_rule

I'm not aware of any pre-processor tools for Python that will syntactically 
check that added braces match the indentation. Such a tool would be 
unPythonic: if they match, the braces are redundant and are not needed, and 
if they do not match, then the compiler (or preprocessor) should not guess 
whether the indentation is right or the braces are right. But if you enforce 
the rule that braces must match indentation, then the braces are redundant!

If you find such a preprocessor, or write your own, feel free to use it. But 
you won't get any respect from experienced Python programmers: we use Python 
in part to get away from braces, we're not chomping at the bit to put them 
back in.

I'm aware that Coffeescript provides a brace-free wrapper around Javascript; 
I'm not aware of any wrapper that *adds* braces to a language without them. 
I suspect such a thing would be of limited use and even less popularity. But 
feel free to try developing one, I could be wrong!



-- 
Steve




More information about the Python-list mailing list