Szorc: Mercurial's Journey to and Reflections on Python 3
Szorc: Mercurial's Journey to and Reflections on Python 3
Posted Jan 14, 2020 0:02 UTC (Tue) by rgmoore (✭ supporter ✭, #75)In reply to: Szorc: Mercurial's Journey to and Reflections on Python 3 by pj
Parent article: Szorc: Mercurial's Journey to and Reflections on Python 3
One of the points made in the blog post, though, is that the creators of Python 3 did some really stupid stuff that made it needlessly difficult to write code that worked in both versions. The specific example that stood out to me was the use of identifiers to specify whether a string literal was a string of bytes or of unicode points. In Python 2, it was possible to specify b''
to say it was a byte string and u''
to say it was a unicode strong. Python 3 kept the b''
syntax but initially eliminated the u''
for unicode strings, and only brought it back when users complained. That hurt people trying to move from Python 2 to Python 3 without providing any benefit to people starting with Python 3.
Posted Jan 14, 2020 10:12 UTC (Tue)
by smurf (subscriber, #17840)
[Link] (14 responses)
What happened instead was an intense period of slowly converting to Py3, heaps of code that use "import six", and modules that ran, and run, with both 2 and 3 once some of those nits were reverted. And they were.
Thus, IMHO accusations of Python core developers not listening to (some of) their users are for the most part really unfounded. Hindsight is 20/20, yes they could have done some things better, but frankly my compassion for people who take their own sweet time to port their code to Python3 and complain *now*, when there's definitely no more chance to add anything to Py2 to make the transition easier, is severely limited.
Posted Jan 14, 2020 15:54 UTC (Tue)
by Cyberax (✭ supporter ✭, #52523)
[Link] (13 responses)
These concerns were raised back in 2008, but Py3 developers ignored them because it was clear (to them) that only bad code needed it and developers should shut up and eat their veggies.
Posted Jan 22, 2020 18:47 UTC (Wed)
by togga (subscriber, #53103)
[Link] (12 responses)
And then obviously removed later on.
Python 2.7.17 >>> b'{x}'.format(x=10)
Python 3.7.5 >>> b'{x}'.format(x=10)
Posted Jan 22, 2020 19:47 UTC (Wed)
by foom (subscriber, #14868)
[Link] (11 responses)
Posted Jan 22, 2020 19:57 UTC (Wed)
by togga (subscriber, #53103)
[Link] (10 responses)
Posted Jan 23, 2020 8:50 UTC (Thu)
by smurf (subscriber, #17840)
[Link] (9 responses)
Posted Jan 23, 2020 14:18 UTC (Thu)
by mathstuf (subscriber, #69389)
[Link] (7 responses)
So instead the burden is put on the coder to have to think about whether bytes or strings will be threaded through their code and can't use the newer API if they might have bytes floating about?
Posted Jan 23, 2020 16:48 UTC (Thu)
by Cyberax (✭ supporter ✭, #52523)
[Link] (6 responses)
Posted Jan 25, 2020 2:24 UTC (Sat)
by togga (subscriber, #53103)
[Link] (5 responses)
There is no reason for not
Posted Jan 25, 2020 11:03 UTC (Sat)
by smurf (subscriber, #17840)
[Link] (4 responses)
Python3 source code is Unicode. Python attribute access is written as "object.attr". This "attr" part therefore must be Unicode. Why would you want to use anything else? If you need bytestrings as keys, or integers for that matter, use a dict.
Posted Jan 25, 2020 21:44 UTC (Sat)
by togga (subscriber, #53103)
[Link] (3 responses)
>Why would you want to use anything else?
>"use a dict."
>>> a=type('A', (object,), {})()
Posted Jan 29, 2020 12:32 UTC (Wed)
by smurf (subscriber, #17840)
[Link] (2 responses)
Sure, if you want to be pedantic you can use "-*- coding: iso-8859-1 -*-" (or whatever) in the first two lines and write your code in Latin1 (or whatever), but that's just the codec Python uses to read the source. It's still decoded to Unicode internally.
> >"use a dict."
Currently. In CPython. Other Python implementations, or future versions of CPython, may or may not use what is, or looks like, a generic dict to do that.
Yes, I do question why anybody would want to use attributes which then can't be accessed with `obj.attr` syntax. That's useless.
Use a (real) dict.
Posted Feb 12, 2020 20:40 UTC (Wed)
by togga (subscriber, #53103)
[Link] (1 responses)
As I said above, it's a necessity due to the design of library APIs. Examples of needed, otherwise unnecessary, encode/decode are plenty (and error-prone). Article mentions a few, I've already mentioned ctypes where for instance structure field names (often read from binary APIs such as c strings, etc) is required to be attributes.
This thread has become a bit off topic. The interesting question for me is Python 2to3 language regressions or which migrations that are feasible, that stage was done in ~ 2010 to 2013 with several Python3 failed migration attempts. Nothing of value has changed since. Half of my Python2 use-cases is not suited for Python3 due to it's design choices and I do not intend to squeeze any problem in a tool not suited for it. That's more of a fact.
The question back of my head for me is about the other half of my use-cases that fits Python3. Given the experience of python leadership attitudes, decisions, migration pains, etc which the article is one example of. Is python3 a sound language choice for new projects?
Posted Feb 12, 2020 20:47 UTC (Wed)
by togga (subscriber, #53103)
[Link]
Oops.. it should read the opposite. "is not" Python 2to3 language regressions
Posted Jan 25, 2020 13:26 UTC (Sat)
by foom (subscriber, #14868)
[Link]
Given the invention of better format syntax, forcing the continued use of the worse/legacy % format syntax for bytestrings seems a somewhat mystifying decision.
It's not as if the only use of bytestrings is in code converted from python 2...
Szorc: Mercurial's Journey to and Reflections on Python 3
Szorc: Mercurial's Journey to and Reflections on Python 3
That wouldn't have worked because Python 3.0 lacked many required features, like being able to use format strings with bytes. They got re-added only in Python 3.5 released in late 2014 ( https://www.python.org/dev/peps/pep-0461/ ). So for many projects realistic porting could begin around 2015 when it trickled down to major distros.
Szorc: Mercurial's Journey to and Reflections on Python 3
'10'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'format'
Szorc: Mercurial's Journey to and Reflections on Python 3
>>> b'%d' % (55,)
was supported again, but *not* the new and recommended format function.
Szorc: Mercurial's Journey to and Reflections on Python 3
Szorc: Mercurial's Journey to and Reflections on Python 3
.format was never "recommended" on bytestrings, in fact it was initially proposed for Python3. Neither was %, but lots of older code uses it in contexts which end up byte-ish when you migrate to Py3. That usage never was prevalent for .format, so why should the Python devs incur the additional complexity of adding it to bytes?
Szorc: Mercurial's Journey to and Reflections on Python 3
Szorc: Mercurial's Journey to and Reflections on Python 3
Szorc: Mercurial's Journey to and Reflections on Python 3
* allowing byte strings as attributes
* being consistent with types and syntax for byte strings and strings
* being consistent with format options for strings and byte strings
* etc.
Szorc: Mercurial's Journey to and Reflections on Python 3
Szorc: Mercurial's Journey to and Reflections on Python 3
Mostly due to library API:s requiring attributes for many thing. This is a big source for py3 encode/decode errors.
This is what attributes does:
>>> setattr(a, 'b', 22)
>>> setattr(a, b'c', 12)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'bytes'
>>> a.__dict__
{'b': 22}
>>> type(a.__dict__)
<class 'dict'>
>>> a.__dict__[b'c']=12
>>> a.__dict__
{'b': 22, b'c': 12}
Szorc: Mercurial's Journey to and Reflections on Python 3
> This is what attributes does:
Also, it's not just bytes, arbitrary strings frequently contains hyphens, dots, or even start with digits.
Szorc: Mercurial's Journey to and Reflections on Python 3
Szorc: Mercurial's Journey to and Reflections on Python 3
Szorc: Mercurial's Journey to and Reflections on Python 3