Reputation: 18735
PyCharm warns me that variable category
can be referenced before assignment but I don't think so.
Except should catch every Exception
(except fatal errors) and finally is called after try
or except
block.
try:
category = lst[2]
except:
category = None
finally:
if not category: #here
category = self.default_category
What do you think? Is it true or is it a bug?
Upvotes: 1
Views: 2036
Reputation: 41872
Perhaps PyCharm is seeing the assignment without considering, "assignment to what". That is, the None
is what makes the difference, consider if you instead wrote:
try:
category = lst[2]
except:
category = Noone
finally:
if not category:
category = self.default_category
(Or None/1
, etc.) Then you'd get:
NameError: name 'category' is not defined
as there would be an exception in the exception if lst
were empty:
When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in an except or else clause), it is re-raised after the finally clause has been executed.
Upvotes: 2