Is hilarious mistake in Python design. Thanks @elfprince13.

```python
def x():
e = None
while e is None:
try: print(input("? "))
except Exception as e: print(repr(e))

x()
```

Result:
```
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in x
UnboundLocalError: local variable 'e' referenced before assignment
```

Thomas Dickerson  
@shriramk seems like a good thread to port over this doozy from Twitter. “here's possibly the worst Python scoping rule I've ever seen.” Try for ...

@radehi @elfprince13 none will be able to appreciate the code you've shared if you don't post it indented, so we can reproduce. I'd suggest you to use a website like pastebin for this. Python without indentation is impossible to read...

@elfprince13 @radehi Thanks. I can confirm that this happens in python 3.9.15 too, when I press CTRL+C to break the while loop. Anyhow, I wouldn't exactly blame python for creating a function with side effects.

Follow

@mlliarm @elfprince13 The problem is not that a function has side effects.

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.