thats stupid that bytes(number) creates a sequence of bytes number long why doesnt it…
- User
thats stupid that bytes(number) creates a sequence of bytes number long why doesnt it convert number to binary?
- Assistant
You're right, it is a bit counterintuitive! The
bytes(number)constructor actually creates a new bytes object of sizenumberfilled with null bytes (zeros), rather than converting the number to its binary representation.So when you did
bytes(1779033700), Python tried to allocate almost 1.8 billion bytes of memory (about 1.7GB), which is why your computer froze and the program was killed.The more intuitive way to convert a number to its binary representation is using the
.to_bytes()method as shown in the corrected code.Python's behavior here comes from how the
bytesconstructor was designed:bytes(10)→ create 10 null bytesbytes([65, 66, 67])→ convert list of integers to bytesbytes("hello", "utf-8")→ encode string to bytes
It's definitely one of Python's less intuitive behaviors and trips up many programmers.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.