Home  >  Article  >  Backend Development  >  Does python support char?

Does python support char?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-19 17:46:206589browse

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.

Does python support char?

>>> 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn