[Tutor] simple unicode question
Ben Finney
ben+python at benfinney.id.au
Sat Aug 23 03:27:07 CEST 2014
Danny Yoo <dyoo at hashcollision.org> writes:
> Hi Albert,
>
> Just following up: I consider the 2 (or 3) arg form of unicode() to be
> a design flaw in the Standard Library. It's redundant because the
> bytes type already has a decode() method:
>
> https://docs.python.org/3/library/stdtypes.html#bytes.decode
>
>
> So I would personally write convert() like this:
>
> ######################################
> def convert(data):
> if isinstance(data, float):
> return unicode(data)
> if isinstance(data, bytes):
> return data.decode("utf-8")
> raise ValueError("Unexpected data", data)
> ######################################
Since the type of ‘data’ is the only thing which determines whether an
error is raised, the error message might be improved by stating the
type::
def convert(data):
if isinstance(data, float):
return unicode(data)
if isinstance(data, bytes):
return data.decode("utf-8")
raise ValueError(
"Unexpected type {type} for data {data!r}".format(
type=type(data), data=data))
which makes the errors more self-explanatory, IMO::
>>> convert(17.3)
'17.3'
>>> convert(b'\x53\x70\x61\x6d')
'Spam'
>>> convert(12)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in convert
ValueError: Unexpected type <class 'int'> for data 12
>>> convert('Spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in convert
ValueError: Unexpected type <class 'str'> for data 'Spam'
--
\ “Anyone who puts a small gloss on [a] fundamental technology, |
`\ calls it proprietary, and then tries to keep others from |
_o__) building on it, is a thief.” —Tim O'Reilly, 2000-01-25 |
Ben Finney
More information about the Tutor
mailing list