Does python care about indents?
Indentation is essential in Python; it replaces curly braces, semi-colons, etc. in C-like syntax. Show
Consider this code snippet, for example:
If it weren't for indentation, we would have no way of indicating what code block the second Mayank Verma Mayank VermaDevOps Engineer at Zeta SuitePublished May 27, 2018 Every programmer who's had experience with C/C++/Java (and many more) understands the pain one has to go through with forced Python Indentations! Now before you scroll down believing this to be one of those juveniles crying over nothing significant, let me tell you that this isn't one of those typical 'Why Python Indentation Sucks' article that thrashes the language developers left and right for introducing an So, what's wrong with indentations?I believe most of you, who've gone through a Python script with a good number of nested loops already know what I'll be talking about. For the uninitiated, let's go through them anyway:
Do the benefits outweigh the flaws?As you might have realized by now, they don't. I've heard from many proponents of the feature that the primary purpose of forced indentation was to curb the bad practice of not indenting code altogether. But at what cost? At the cost of cultivating new bad practices like discussed above? I don't think so. Some you might suggest that most popular IDEs take care of indentation, but what about those who'd rather prefer to write on Notepad (Okay, I don't write on that dreaded editor, but let's not forget about the people who do) or gedit, or vim without any plugins? Hey crying baby! Are you done ranting about it? So what if there're problems? Is there anything we can do without breaking things?Yes we can. Actually, I've already attempted to do something about it. It's called PyBrace. PyBrace is an extension of the original Python language that adds support for braces! You can write your nested compound statements in the way you expect with other languages with native support for braces. You can check out the project here: https://github.com/mayank-verma048/PyBrace So an extended version of an existing language, huh? How're we supposed to share our completely non-compliant code with other people? You only write your code in PyBrace. It doesn't mean you have to share it in PyBrace. PyBrace toolkit provides a utility called pyb, which is a transpiler that converts PyBrace code to vanilla Python script. So, while you can write your code in PyBrace, others can read and work with vanilla Python script thanks to the transpiler.
So, check out PyBrace right now and save yourself from Indentation hell! Others also viewedExplore topicsDo indents matter in Python?Indentation is a very important concept of Python because without proper indenting the Python code, you will end up seeing IndentationError and the code will not get compiled.
Does Python rely on indentation?Python, however, uses indentation. A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line. The amount of indentation is up to you, but it must be consistent throughout that block.
How does Python handle indentation?How to solve an indentation error in Python?. Check for wrong white spaces or tabs. ... . Be certain that the indentation for a specific block remains the same throughout the code, even if a new block is introduced in the middle. ... . Go to your code editor settings and enable the option that seeks to display tabs and whitespaces.. What happen if we skip indentation in Python?Python is a language where the code is arranged through whitespaces. If there is an incorrect indentation, this will result in an error, and the python interpreter will just return an error function. It uses the PEP8 whitespace ethics. There should be 4 whitespaces used between any alternative or iteration.
|