(I am a beginner) Python normally uses indentation to specify the nesting level of code lines. Is there any other way to do this?
Asked
Active
Viewed 932 times
0
-
8Why would you want to do that? – Brian Cain Jun 21 '13 at 15:36
-
Your question lacks actual code, -1. **Show us your code.** – ulidtko Jun 21 '13 at 15:42
-
6@ulidtko What code do you expect him to show? – Paul Manta Jun 21 '13 at 15:43
-
@Paul any one, which he has troubles with. That way we could argue by example. – ulidtko Jun 21 '13 at 15:44
-
3@ulidtko It doesn't apply here. – Paul Manta Jun 21 '13 at 15:47
-
Also, not providing any problematic code for a question displays lack of effort from the asker. Questions without code are less useful, in general. – ulidtko Jun 21 '13 at 15:47
-
1[This other question](http://stackoverflow.com/questions/63086/is-there-a-way-around-coding-in-python-without-the-tab-indent-whitespace-crit) was asked long ago, to be fair. – icedwater Jun 21 '13 at 15:49
-
@Paul, it easily does. For example, imagine a badly indented C++ snippet being asked for a rough Python translation. – ulidtko Jun 21 '13 at 15:50
-
What about replacing indentation with a nesting level number for each line? – ThePiercingPrince Jun 22 '13 at 00:44
-
@LinuxDistance: Seriously? You think starting each line with a nesting level number is **better** than simply starting each line with that number of indentation levels?!?!?!?! That is crazier than adding braces (or begin..end) to Python! – John Y Jul 05 '13 at 13:39
3 Answers
11
No, the Python developers are very resistant to this, as it would mean changing one of the core foundations on which Python was based. Just try from __future__ import braces
.
>>> from __future__ import braces
SyntaxError: not a chance (<pyshell#30>, line 1)
Indeed, "not a chance" :-)

arshajii
- 127,459
- 24
- 238
- 287
-
I know that brackets for code blocks will not be implemented, but is there any way other than indentation or braces to define code blocks or instead I will end up writing very complicated code. – ThePiercingPrince Jun 21 '13 at 15:41
-
2@LinuxDistance What other way could there be? And how can this effect the complexity of your code? – arshajii Jun 21 '13 at 15:42
-
7@LinuxDistance Your code isn't going to be bad because of indentation. If you think that's the case then you need to take a serious look at how your code is organized. – bdesham Jun 21 '13 at 15:43
-
1@arshajii: There is a third way. In ruby and vbscript many constructs (looping, if statements, function definitions, etc) effectively act as an opening brace that needs to be explicitly ended with a keyword (such as `end`, `End If`, `End Sub`). I initially thought of this as a variant on braces, but the implicit "opening brace" makes it different. – Steven Rumbalski Jun 21 '13 at 15:57
-
@StevenRumbalski Yes you are right, I forgot about that variant. Nevertheless, there's really only one way to do it in Python. – arshajii Jun 21 '13 at 17:26
1
The core design philosophy behind Python is human readability; indentation is used to specify code blocks because visually, this is significantly cleaner than the use of braces.
For more info, see PEP 20 - The Zen of Python.

Nick Peterson
- 761
- 7
- 20
0
Until I find that thing I glanced at a while ago, a pointer to this question is going to have to suffice as an answer: consider using reindent.py
to fix your indents after writing the code however you want.
Pick it up from this site.