News for hackers. Stuff that matters.
Re:Why Python 3 Exists by bpicolo on Thursday December 17, 2015 @12:41PM (#10752928)
Disagree. The original commenter is correct in saying that the naming scheme is not obvious. Something like to_bytes(encoding) would be a lot more clear.
Re:Why Python 3 Exists by the_mitsuhiko on Thursday December 17, 2015 @12:52PM (#10753008)
> Disagree. The original commenter is correct in saying that the naming scheme is not obvious. Something like to_bytes(encoding) would be a lot more clear.

But then function would do something completely different. "\x01\x02".encode('zlib') for instance is a bytes to bytes operation. The problem is that "foo".encode('utf-8') does not give you an exception. If the coercion would not be enabled you would get an error:

    >>> reload(sys).setdefaultencoding('undefined')
    >>> 'foo'.encode('utf-8')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/Cellar/python/.../encodings/undefined.py", line 22, in decode
        raise UnicodeError("undefined encoding")
    UnicodeError: undefined encoding
That's not any worse than what Python 3 does:

    >>> b'foo'.encode('utf-8')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'bytes' object has no attribute 'encode'
Re:Why Python 3 Exists by kenko on Thursday December 17, 2015 @02:03PM (#10753616)
> "\x01\x02".encode('zlib') for instance is a bytes to bytes operation.

I think the inclusion of things like zlib (or rot13 or whatever) was a conceptual error that just fosters confusion.

Re:Why Python 3 Exists by the_mitsuhiko on Thursday December 17, 2015 @03:53PM (#10754465)
> I think the inclusion of things like zlib (or rot13 or whatever) was a conceptual error that just fosters confusion.

We should not optimize languages for idiots. There is nothing confusing about such an operation for anyone who can use their brain. Python 3 still contains those operations but instead of x.encode(y) you now do encode(x, y).

Re:Why Python 3 Exists by bpicolo on Friday December 18, 2015 @12:00AM (#10756721)
The principle of least astonishment and optimizing for idiocy are not the same thing.
Re:Why Python 3 Exists by the_mitsuhiko on Friday December 18, 2015 @04:38AM (#10757573)
How is an attribute error clearer than an exception that says something like: operation does not make sense of this type?
Re:Why Python 3 Exists by bronson on Thursday December 17, 2015 @03:21PM (#10754228)
"\x01\x02".encode('zlib') is a really odd API. Just making sure: this is a real thing, and you're in favor of it?
Re:Why Python 3 Exists by tveita on Friday December 18, 2015 @07:35AM (#10758102)
It is in Python 2, but not in Python 3, where str.encode() and bytes.decode() have been restricted to only convert between strings and bytes.

    >>> "foo".encode('zlib')
    LookupError: 'zlib' is not a text encoding; use codecs.encode() to handle arbitrary codecs
Re:Why Python 3 Exists by the_mitsuhiko on Thursday December 17, 2015 @03:54PM (#10754473)
> "\x01\x02".encode('zlib') is a really odd API. Just making sure: this is a real thing, and you're in favor of it?

Of course this is a real thing and it's very frequently used. Yes I am in favour of it as the codec system in Python is precisely the place where things like this should live.