Crappy Python code of the day
Chris Angelico
rosuav at gmail.com
Wed Feb 15 22:28:57 EST 2017
On Thu, Feb 16, 2017 at 1:25 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> This has been in production for months, the writer of the code has left
> and the new maintainer has been asked to find out why it has been
> crashing with UnboundLocalError:
>
>
> try:
> result = future.result()
> except requests.exceptions.ConnectionError as e:
> pass
> resp = self.client.service.GetModifiedTerminals(
> __inject={'reply': result.content})
>
>
> Traceback (most recent call last):
> [incriminating details removed for the protection of the guilty]
> UnboundLocalError: local variable 'result' referenced before assignment
>
Without even looking at the link.... 'except pass' around an
assignment. Unless there's a preceding "result = some-other-object",
that's going to annoyingly fail. Maybe "except return None"?
ChrisA
More information about the Python-list
mailing list