The Zen of Python Article 3
Simple is better than complex.
In Python, the space occupied by each character in a string is 8 bits.
>>> import sys >>> sys.getsizeof('') 37 >>> sys.getsizeof('a') 38
As you can see, the null character occupies 37 bytes, and the string 'a' with a length of 1 occupies 38 bytes of memory. There is one more character a. Then there is 1 more byte.
Related recommendations: "Python Video Tutorial"
Each char is stored in ob_sval, accounting for 8 bits in size. The remaining 36 bytes mainly come from Macro PyObject_VAR_HEAD. In fact, Python's string implementation also uses a global variable called *interned, which can store strings with a length of 0 or 1, that is, char, which can save space and speed up.
In fact, there are neither pointers nor "naked data structures" (non-objects) in python, even the simplest integer integer is implemented in this way.
In short, this design satisfies Python's design philosophy of "everything is an object" and "everything is as simple as possible".
The above is the detailed content of Does python support char?. For more information, please follow other related articles on the PHP Chinese website!