IndentationError: unexpected indent after comment

Question:

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

Asked By: Ray Hulha

||

Answers:

Did you try this?

user_by_email = session.query(User).
filter(Address.email=='one').
#options(joinedload(User.addresses)).
first()
Answered By: Vasily Bronsky

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))
Answered By: Morse

Enclose the statement in parentheses.

user_by_email = (session.query(User)
     .filter(Address.email=='one')
     #.options(joinedload(User.addresses))
     .first())
Answered By: Sreyas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.