Reputation: 4857
I am a newbie to python and I have an indentation error message when I commented ('#') the two last lines of the following code :
try:
return get_callable(callback), {}
# except (ImportError, AttributeError), e:
# raise ViewDoesNotExist("Tried %s. Error was: %s" % (callback, st r(e)))
Can anybody help ?
Upvotes: 0
Views: 108
Reputation: 1121266
When commenting out a try
/ except
, place a if True: #
in front of the try
:
if True: #try:
return get_callable(callback), {}
# except (ImportError, AttributeError), e:
# raise ViewDoesNotExist("Tried %s. Error was: %s" % (callback, st r(e)))
This makes for correct syntax without having to de-dent the inner block. You could also add a finally: pass
block after the commented except
:
try:
return get_callable(callback), {}
# except (ImportError, AttributeError), e:
# raise ViewDoesNotExist("Tried %s. Error was: %s" % (callback, st r(e)))
finally:
pass
Your only other option is to comment out the try:
line as well, and remove the indentation of the block:
# try:
return get_callable(callback), {}
# except (ImportError, AttributeError), e:
# raise ViewDoesNotExist("Tried %s. Error was: %s" % (callback, st r(e)))
You cannot leave a bare try:
in place without an except
or finally
block to complete it.
Upvotes: 5
Reputation: 10116
If there is not another except
statement, python is looking for an except statement and instead likely sees an unindented line.
So you might think, "why is this an indentation error? I'm just missing an except
, which doesn't have anything to do with indents." The reason is that python "sees" an unindented line following a try:
and expects it to be indented to be inside the try
.
Upvotes: 2
Reputation: 49816
Your code is no longer syntactically valid. The except clause is a required companion of the try clause.
Upvotes: 4