4 ms·
> In Python a function can fail with any error and there is nothing you can do about that. To be fair, the adorable `except:` black magic exists there to be us
by barosl 11y ago
> In Python a function can fail with any error and there is nothing you can do about that.
To be fair, the adorable `except:` black magic exists there to be used in such a case. Of course its use is extremely dangerous for obvious reasons (it even catches the `SystemExit` exception!), so I can understand why the author didn't mention this.
> This is a big change in how you think about programs but you will get used to it.
Possibly! But I have a feeling that the ownership model would be the biggest blocker to newcomers, as well as the largest innovation Rust brought to the mainstream languages. I can already hear the crowd saying Rust's just a giant mess, after struggling with the borrow checker. This may impact the success of the language. Now that Rust is post-1.0, we will see.
> the constant encoding and decoding you have to deal with in Python just to support O(1) string indexing.
Strictly speaking, Python also doesn't support a complete character indexing, because it works on code point basis. O(1) string indexing is largely a myth.[1] But I'm sure that the author definitely knows about this, considering his previous rants on Python's Unicode handling. ;)
Regarding Unicode handling, I think Swift's `String` type is arguably more complete than Rust or Python. Its internal representation is UTF-16, possibly due to the interoperability concerns with `NSString`, so it can't compete with Rust on the performance aspect. However, it indexes a string by grapheme cluster (so Swift's `Character` type is actually a slice, not an integer) and compares the characters by their normalized forms, which eliminates many mistakes that can be made by careless programmers, including me. Of course the same thing can be done in other languages using external libraries, but the official support residing in the built-in string type is very nice to have.
[1] https://hamstergene.github.io/post/wchar-is-useless/ https://hamstergene.github.io/post/wchar-is-useless/
- masklinn 11y ago> To be fair, the adorable `except:` black magic exists there to be used in such a case. Of course its use is extremely dangerous for obvious reasons (it even catches the `SystemExit` exception!) And KeyboardException. Which is why it's very strongly recommended to never write bare excepts in Python (unless you're going to rethrow immediately and just want to cleanup or log on exception), and always `except Exception` at least. > Regarding Unicode handling, I think Swift's `String` type is arguably more complete than Rust or Python. Very likely considering it inherits Apple's concern for internationalisation, Objective-C was one of the very few languages which exposed relatively easy grapheme cluster level manipulations. I believe Rust's string mostly tries to provide a sensible interface, not a complete one, which is why you can iterate on bytes and codepoints (decoded on the fly) but more complex unicode operations tend to be left to external libraries. Whether that's a good idea is debatable, on the one hand implementing fully unicode aware strings would make that part of the standard library much more complex and bigger, on the other hand not doing so means you still get mostly broken strings when it comes to end-user output.
- deleted 11y ago[deleted]
- crdoconnor 11y ago>To be fair, the adorable `except:` black magic exists This is not adorable. This is a license to make programmers who come after you experience a kind of living hell.
- amyjess 11y agoOh, I'm fairly sure my unholy abuse of decorators and dynamic __import__() trumps any time I've ever used a bare 'except:' in the past (and I think I got rid of all my old bare 'except:'s during a code cleanup anyway).
- Grue3 11y ago>To be fair, the adorable `except:` black magic exists there to be used in such a case. Of course its use is extremely dangerous for obvious reasons (it even catches the `SystemExit` exception!), so I can understand why the author didn't mention this. This is why you use "except Exception:" which doesn't catch system exceptions.
- themartorana 11y agoI think the point was (and I'm extrapolating here) that `except:` exists at all, and doesn't require any special foresight to use. That compared to Rust, which thinks about safety first. Or so it seemed to me.
- wyldfire 11y ago"except Exception:" is rarely what you want IMO. It also catches SyntaxError, which is almost always a design error -- one you'd typically prefer would halt the system. Unfortunately there's no common parent to EnvironmentError/SystemError/RuntimeError/etc that excludes SyntaxError.
- mixmastamyk 11y agoI can't seem to repro that, SyntaxError seems to happen before the code runs the first time. (2.7 Ubuntu)
- wyldfire 11y agoSyntaxError can happen as a consequence of loading a module (hitting an import statement, etc). But for a simpler example, replace SyntaxError with NameError and it's still almost always a design error that you probably have no sane way to handle in an exception handler (other than to mask the problem). Python's a very dynamic language and so of course you could come up with examples to the contrary (ones where the user input could trigger SyntaxError or NameError).
- techdragon 11y ago