Python Forum
what makes bytes so difficult to do?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what makes bytes so difficult to do?
#1
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>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
try

decimal.Decimal(b'345'.decode())
Gribouillis likes this post
Reply
#3
so the question should be: what makes decode() so difficult?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
The question seems to be: why is the conversion from bytes to Decimal not supported?
Reply
#5
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
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.
DeaD_EyE likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What makes a good programmer? j.crater 5 3,725 Dec-08-2017, 05:08 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020