Follow

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 ...

@freemo why is the formatting in this post broken? The pseudo-Markdown implementation seems to be eating all the newlines. How does that happen and how can we fix it?

@AccordionGuy the formatting in that post looks broken to you too, right? It's not only broken on my instance but in the actual post? Haven't yet figured out how to view the JSON-LD source.

@radehi @freemo

FWIW The only way I got the listing to format correctly posting to myself was 1) post in plain text mode 2) in markdown mode, prepend 4 spaces to each line (which sucks, obviously)

4 backticks didn't work. code tag didn't work.

@ambihelical @freemo By "plain text mode" mean "not Markdown mode"? I don't have a problem with prepending four spaces to each line (that's standard Markdown, fenced code blocks are a common extension) but that didn't keep the buggy implementation from eating my newlines.

Let's try four spaces:

def main():
pass

@ambihelical @freemo See, there was a newline after the colon, but got eaten. Inspecting the page DOM shows that there's a `<pre>` and a somewhat unnecessary but harmless `<code>` inside it, but the actual text node in the DOM has no newline in it.

@radehi Yes, but I just assumed it was one of those “formatting losses” that happens when you paste code into non-code places.

@AccordionGuy It showed up in a typewriter font, though? In theory, this version of Mastodon supports Markdown, but it's pretty broken.

@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...

@mlliarm @elfprince13 Yes, is a bug in the Markdown support in my instance, am talking to the instance admin to try to figure out what we need to to do fix it. Thanks for the report 😁

@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.

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

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.