Python string Object

Source: Internet
Author: User

The integer object of the previous article is the fixed-length data, which is a long integer in C. But Python also has a large number of long-lived data types. such as a String object

As with an integer object, the string object consists of the following:

Pystringobject is defined as follows:

Ob_size is the actual amount of memory used, in the note: Ob_sval contains space for ' ob_size+1 ' elements. Because the end requires an extra 0 byte to guarantee the end of the string. This should not be unfamiliar to the C language. But this is just a character array, how can it contain n characters? Obviously this can only be a character pointer, pointing to a string memory address. This string of memory stores the character contents of a Python string object, apparently with an additional ' 0 '. The actual size of this memory is maintained by Ob_size+1.

The definition of pystring_type is not affixed to the code, and the format is basically the same as an integer object.

Create Pystringobject Object

Pystringobject object creation is not like a function pyint_fromlong that is eventually called by an integer object. So will the string have a cache pool like an integer object? In the Pystring_fromstring function, the original author adds a comment/* share short strings */. When the character is only 1, it is put into the characters. The size of this buffer is Uchar_max. Obviously this is a platform-related macro definition.

From his statement, it can be known that characters is a static variable. According to the C-language approach, after Python finishes initializing, characters should be initialized to an empty array of pointers. Follow the integer object before the print operation. We print the last 5 digits of the characters and their respective reference numbers

When writing this function, by returning void on the book, the compilation will fail and an int must be returned to

You can see an increase in the reference count of a, which is indeed placed in a character array for shared use

Python string Object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.