Begin and End in Python blocks

Question:

I am using Python

but the space gap is making my life very hard with it

example

when I use the if statement

if Parm2 == 1:
    Ch = "A"
elif Parm2 == 2:
    Ch = "B"
elif Parm2 == 3:
    Ch = "C"
else:
    continue
mdl = CallFunc(Parm2)

print("XX Always Print XX")

now the "XX Always Print XX" should be printed regardless

but due to my mistake it is inside the if statement which cause me long time to find

the actual if statement is nested and longer

I wonder if there is a method I can use begin/end or {} in such statements in Python

something like

UPDATE

for the people who focus on the IF statement

if Parm2 == 1:
{
    Ch = "A"
}
elif Parm2 == 2:
{
    Ch = "B"
}
elif Parm2 == 3:
{
    Ch = "C"
}
else:
{
    mdl = CallFunc(Parm2)
}
print("XX Always Print XX")

Happy now??

ok now how to get the brackets work in Python?

Asked By: asmgx

||

Answers:

Python is indentation based. Yeah, its harder to read and easier to make mistakes like you indicated, but that’s what it is.

Answered By: SledgeHammer

Think about downloading an IDE for python, like Pycharm, they are helpful for identifying errors like this one, they also have an "auto-indent" feature. But no, Python is indentation based.

What the Python designers realized is that braces or begin/end keywords are mostly noise to human programmers. They generally recognize the structure of code by its layout. For instance, if you were to write the C code:

if (condition)
    x = y;
    w = z;

a human would often not notice that the braces are missing, and assume that both assignments are controlled by the condition. Writing code like this is a common error, especially when you start with a block that has just one statement (so the braces are optional and were omitted), and forget to add braces when a second statement is added. (See Why is it considered a bad practice to omit curly braces?).

Conversely, if you write

if (condition) {
    x = y;
w = z;
}

it looks like w = z; is not part of the conditional.

Braces mainly exist for the benefit of software that processes code (compilers, editors, IDEs), they make it easier for them to detect groups of code. The Python designers decided to mirror the way humans read code in their parser, rather than forcing humans to adapt to the computer’s needs.

Braces allow for more flexible code layout, but in practice it’s usually condiered wrong to take advantage of this. Writing code like

while (something) { statement1; statement2; 
statement3; }

is less readable than

while something:
    statement1
    statement2
    statement3

Python does allow some flexibility: You can separate statements on the same line with ;, and put the contents of a conditional on the same line after the :. But writing like this is not considered Pythonic, and should be used only in very special circumstances (this blog post describes those cases).

There’s always some adjustment necessary when you’re learning a new programming language and you’re accustomed to the patterns of the languages you previously used (many programmers have refused to learn Lisp, because of its Lots of Irritating, Stupid Parentheses). But give it a little time and you’ll get used to it.

Answered By: Barmar

This shows how you can hack it:

if True: {
    print("hello")
}

If you do a google search for "python what if you don’t want to use indentation for blocks" you might get:
peach pit python indentation

Programmers familiar with other languages often bristle at the thought that indentation matters: Many programmers like the freedom to format their code how they please. However, Python indentation rules are quite simple, and most programmers already use indentation to make their code readable. Python simply takes this idea one step further and gives meaning to the indentation.

i.e. they force you not to use indentation.

My problem with this is that I love to use emacs auto-indentation to reindent the whole code file but this totally screws up the indentation in python; in C or C++ this finds the indentation problems and makes them evident; in python it loses all your information and changes the meaning of the program;

Don’t get me wrong I want to use BOTH rigorous indentation AND curly braces;

You can use the hack above to "circumvent" python indentation but when writing code for anyone other than yourself it won’t be popular.

Answered By: William Garber
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.