Python中print二进制处理机制、bytes函数原理及存储显示疑问
Let’s break down your questions one by one, starting with how print() deals with binary data, then the bytes function, and finally your code examples and memory storage questions.
How print() Displays bytes Objects
When you pass a bytes object to print(), Python doesn’t output raw binary bits directly—it uses a human-readable format:
- It adds a
bprefix to explicitly signal this is abytestype (not a regular string). - For byte values that map to printable ASCII characters (like letters, digits, or common symbols), it shows the character inside quotes.
- For non-printable or non-ASCII bytes, it uses escape sequences (e.g.,
\x00for null,\nfor newline).
This balances clarity about the data type with readability for familiar characters.
Understanding the bytes() Function
The bytes() function creates an immutable sequence of bytes (each byte is an integer between 0 and 255). Here’s how it works in common scenarios:
- Iterable of integers:
bytes([100])converts each integer in the list to a byte (values outside 0-255 throw an error). - String with encoding:
bytes('d', 'ascii')encodes the string into bytes using the specified encoding. - Literal shortcut:
b'd'is a shorthand for creating abytesobject containing the ASCII byte for 'd' (decimal value 100).
Breaking Down Your Code Examples
Let’s walk through your snippets and clear up the confusion around "binary assignment."
Example 1: b'd' and a = bytes([100])
Running this code:
print(type(b'd')) # Output: <class 'bytes'> print(b'd') # Output: b'd' a = bytes([100]) print(type(a)) # Output: <class 'bytes'> print(a) # Output: b'd'
Both b'd' and bytes([100]) create identical bytes objects. The single byte inside has a decimal value of 100, which maps to the ASCII character 'd'—so print() uses b'd' as the readable representation.
Clarifying the "Binary Assignment" Misconception
You mentioned assigning "binary 1100100" as a = 1100100, but that’s a common mix-up:
1100100is just a decimal integer (110,010 in base 10), not a binary type. Itstype()would beint, notbytes.- To write a binary literal integer, prefix it with
0b:a = 0b1100100(this equals decimal 100). Even then,type(a)is stillint, notbytes.
If you want a bytes object from the binary value 0b1100100, use a = bytes([0b1100100])—this is identical to bytes([100]), so print(a) will still output b'd'.
How bytes Objects Are Stored in Memory
- Under the hood, all computer data is stored as binary (0s and 1s). A
bytesobject is a sequence of 8-bit values—so the byte for 'd' (decimal 100) is stored as the 8-bit binary01100100(leading zero to fill 8 bits). - Hexadecimal representations (like
0x64for decimal 100) are just human-readable shorthand—computers don’t store data in hex. Hex is useful because each digit represents 4 bits, making it easier to read than long binary strings. - Python’s
bytesobject doesn’t "store" in hex or decimal—those are just ways to display the underlying binary data to humans.
内容的提问来源于stack exchange,提问作者KMG

