Python Forum

Full Version: what makes bytes so difficult to do?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
what makes bytes so difficult to do? or is it an issue with Decimal type?
Output:
lt2a/forums /home/forums 6> python3 Python 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import decimal >>> int(b'345') 345 >>> float(b'345') 345.0 >>> decimal.Decimal(b'345') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: conversion from bytes to Decimal is not supported >>> lt2a/forums /home/forums 7>
try

decimal.Decimal(b'345'.decode())
so the question should be: what makes decode() so difficult?
The question seems to be: why is the conversion from bytes to Decimal not supported?
decimal.Decimal never accepted bytes, but int accept bytes.
I think the expectation comes from the implementation of int.

Don't forget, that different modules have different developers with different opinions of how it should work.
Have a look at this related issue and especially at the link to mailing list discussion in the last post.

Guido's opinion on whether int() and float() should accept bytes:
Guido van Rossum Wrote:Yeah, practicalibty beat purity on that one. I'd say let it beat purity on int() and float() as well.

In other words int() and float() implementation in this respect should be considered the exception, not the norm.
what does "beat purity" mean?

is there a general statement on what bytes are for?

for everything i create that takes str, i generally tend to make it also at least accept bytes and maybe also bytearray. that way, if there is a case i happen to be working in bytes, i still have use of almost everything i have created.