0

(I am a beginner) Python normally uses indentation to specify the nesting level of code lines. Is there any other way to do this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ThePiercingPrince
  • 1,873
  • 13
  • 21

3 Answers3

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.

Community
  • 1
  • 1
icedwater
  • 4,701
  • 3
  • 35
  • 50