Python Forum
what makes bytes so difficult to do? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: what makes bytes so difficult to do? (/thread-35710.html)



what makes bytes so difficult to do? - Skaperen - Dec-04-2021

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>



RE: what makes bytes so difficult to do? - Axel_Erfurt - Dec-04-2021

try

decimal.Decimal(b'345'.decode())


RE: what makes bytes so difficult to do? - Skaperen - Dec-05-2021

so the question should be: what makes decode() so difficult?


RE: what makes bytes so difficult to do? - Gribouillis - Dec-05-2021

The question seems to be: why is the conversion from bytes to Decimal not supported?


RE: what makes bytes so difficult to do? - DeaD_EyE - Dec-05-2021

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.


RE: what makes bytes so difficult to do? - buran - Dec-06-2021

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.


RE: what makes bytes so difficult to do? - Skaperen - Dec-07-2021

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.