[Python-Dev] bytes.from_hex() [Was: PEP 332 revival in coordination with pep 349?]

"Martin v. Löwis" martin at v.loewis.de
Fri Feb 17 23:52:15 CET 2006


Josiah Carlson wrote:
> How are users confused?

Users do

py> "Martin v. Löwis".encode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 11:
ordinal not in range(128)

because they want to convert the string "to Unicode", and they have
found a text telling them that .encode("utf-8") is a reasonable
method.

What it *should* tell them is

py> "Martin v. Löwis".encode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'encode'

> bytes.encode CAN only produce bytes.

I don't understand MAL's design, but I believe in that design,
bytes.encode could produce anything (say, a list). A codec
can convert anything to anything else.

> If some users
> can't understand this (passing different arguments to a function may
> produce different output),

It's worse than that. The return *type* depends on the *value* of
the argument. I think there is little precedence for that: normally,
the return values depend on the argument values, and, in a polymorphic
function, the return type might depend on the argument types (e.g.
the arithmetic operations). Also, the return type may depend on the
number of arguments (e.g. by requesting a return type in a keyword
argument).

> then I think that some users are broken beyond repair.

Hmm. I'm speechless.

Regards,
Martin


More information about the Python-Dev mailing list