Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python indentation in "empty lines"

Which is preferred ("." indicating whitespace)?

A)

def foo():     x = 1     y = 2 ....     if True:         bar() 

B)

def foo():     x = 1     y = 2      if True:         bar() 

My intuition would be B (that's also what vim does for me), but I see people using A) all the time. Is it just because most of the editors out there are broken?

like image 749
nisc Avatar asked Apr 28 '10 08:04

nisc


People also ask

Why are some lines indented in Python?

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.

How do I avoid indentations in Python?

How to Avoid Indentation Error in Python? In order to not have an indentation error in Python, you must go through every line of your code separately. This will help you in understanding which line contains the error or does not have the expected whitespaces. Python arranges all lines of codes in the form of blocks.

Are Blank lines forbidden in Python?

Blank line is definitely allowed in python functions. All of yours examples (A,B,C,D) should work and the problem probably (sure) is related to "Eclipse and the PyDev plug-in".


1 Answers

If you use A, you could copy paste your block in python shell, B will get unexpected indentation error.

like image 50
YOU Avatar answered Oct 11 '22 00:10

YOU