Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndentationError: unexpected indent after comment [duplicate]

I am trying to write some Python example code with a line commented out:

user_by_email = session.query(User)\
    .filter(Address.email=='one')\
    #.options(joinedload(User.addresses))\
    .first()

I also tried:

user_by_email = session.query(User)\
    .filter(Address.email=='one')\
#    .options(joinedload(User.addresses))\
    .first()

But I get IndentationError: unexpected indent. If I remove the commented out line, the code works. I am decently sure that I use only spaces (Notepad++ screenshot):

enter image description here

like image 699
Ray Hulha Avatar asked Jun 06 '18 13:06

Ray Hulha


People also ask

How do I fix IndentationError unexpected indent error?

“IndentationError: unexpected indent” is raised when you indent a line of code too many times. To solve this error, make sure all of your code uses consistent indentation and that there are no unnecessary indents.

Why does Python keep saying unexpected indent?

1. Unexpected indent - This line of code has more spaces at the beginning than the one before it, but the one before it does not begin a subblock. In a block, all lines of code must begin with the same string of whitespace.

How do I fix IndentationError in Python?

Go to the setting of your code editor and enable the option which shows the tab and whitespaces. Once this feature in turned on, you will see small single dots in between your code, where each dot represents whitespace or a tab.

How do I fix the TabError in Python?

The Python “TabError: inconsistent use of tabs and spaces in indentation” error is raised when you try to indent code using both spaces and tabs. You fix this error by sticking to either spaces or tabs in a program and replacing any tabs or spaces that do not use your preferred method of indentation.


2 Answers

Enclose the statement in paranthesis

user_by_email = (session.query(User)
     .filter(Address.email=='one')
     #.options(joinedload(User.addresses))
     .first())
like image 65
Sreyas Avatar answered Sep 28 '22 22:09

Sreyas


Essentially its the same line , thats how Python interpreter reads it.

Just like you can not comment just a word in line of code. (Below)

Not allowed

user_by_email = session.query(User).filter(Address.email=='one')#comment#.first()

You need to move the comment to the end of the line.

user_by_email = session.query(User)\
    .filter(Address.email=='one')\
    .first()
#.options(joinedload(User.addresses))\
like image 25
Morse Avatar answered Sep 28 '22 21:09

Morse