New to Python - block grouping (spaces)

BartC bc at freeuk.com
Thu Apr 16 17:04:35 EDT 2015


On 16/04/2015 18:10, Steven D'Aprano wrote:
> 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

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

I meant hitting Backspace or Delete.

But also, sometimes you post code to Usenet and you find leading tabs 
have been stripped out. With Python code, that's problematical.

> 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?

Take:

  if cond:
     stmt1
     stmt2
     stmt3

and:

  if cond:
     stmt1
     stmt2
  stmt3

which one is correct? Is there a tab too many, or one too few? Or two 
too many? Now, much as I dislike C-style braces, at least it is a little 
more resilient:

  if (cond) {
     stmt1;
     stmt2;
  stmt3;
  }

or:

  if (cond) {
     stmt1;
     stmt2;
  }
     stmt3;

One 'if' clearly has three statements, and the other has two, and you 
are confident enough to fix the tabbing errors without consequences.

> 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 seems to work well enough with expressions:

  x = (a * (b+c) * d) / e

-- 
Bartc



More information about the Python-list mailing list