4 ms·
It's well known that you should make the "correct" choice the default. So unicode should have been opt-out (from __past__ import legacy_strings). Result: Everyo
by im3w1l 5y ago
It's well known that you should make the "correct" choice the default. So unicode should have been opt-out (from __past__ import legacy_strings). Result: Everyone batch-adds unicode opt-out to their whole codebase, but new files use the modern way. Gradually people remove the opt outs as they go over old code.
- TheCoelacanth 5y agoThe correct way should be the default, but in the case where you already chose the wrong default, you shouldn't introduce the correct way of handling things and make it the default all at once. Upgrading to a new major version should never require changes to your code if you fixed all deprecation warnings in the previous major version. Otherwise, you make incremental upgrades impossible. The new alternative should always be introduced as opt-in with the old default deprecated, and then made default in the next major version.
- pwdisswordfish8 5y agoThis couldn’t work. For starters, the names of classes, attributes and functions are str in both versions, which is to say, bytestrings in Python 2 and Unicode strings in Python 3. This cannot be handwaved away with a pragma.
- im3w1l 5y agostr = int is perfectly valid python. So having str mean different things in different modules is already possible.
- pwdisswordfish8 5y agoYou missed the point. It’s not about names themselves. What happens if I want to lookup bytestring-named attributes on a class defined in a Unicode-string module or vice versa?
- im3w1l 5y agoI'm afraid I don't understand your question completely, so I'll answer what I guess you mean. Let's say you have a function in one module that expects a bytestring. This function is called from another module, and a unicode string is passed. What happens? Well python is a duck-typed language, so it will handle the unicode string as if it was a bytestring, and this may or may not work correctly. It's up to the modern code to interface correctly with the legacy code. This could mean manually converting between bytestring and unicode string.
- ByteJockey 5y agoThat ignores how python scoping works. Built-ins are actually the last thing python checks. You can override the built-ins by re-defining the symbol in any of: local functions, enclosing functions, or the global scope and it will impact the entirety of that scope.
- pwdisswordfish8 5y agoYou missed the point. It’s not about names themselves. What happens if I want to lookup bytestring-named attributes on a class defined in a Unicode-string module or vice versa?
- ByteJockey 5y agoAhh, the wording screwed me up. If I'm understanding your comment correctly this time, it would look something like: getattr(thing, b'attribute_name')
- im3w1l 5y agoI guess you could make it work as long as everything is ascii right? And if it's not then raise exception.
- ByteJockey 5y agoIf it's not ascii, you would just do: getattr(thing, u'attribute_with_unicode_characters') This would work in both python 2.7 and 3.9 (probably works with other 3.X versions but it's the one I actually tried).