Elaborate on PyString Object source code

Source: Internet
Author: User

Python is also known as a very thorough language. When the designer designed it at the beginning, the general guiding ideology is for a specific problem, as long as there is one of the best ways to solve the problem, it is OK. Next we will describe the Python PyStringObject object.

Different from a fixed-length object, the Data Length maintained by the object is unknown when the object is defined. For py1_bject, the length of the data maintained by it is determined when the object is defined. Is the length of a long variable. The length of the data maintained by the variable object can only be determined when the object is created. Consider the following, we only know the length of the data they maintain when creating a string or a list. Before that, we knew nothing about this information.

In a variable-length object, it can also be divided into mutable and immutable. After the object is created, the length of the maintained data can also be changed. For example, after a list is created, you can add or delete elements to it.

These operations change the length of the maintained data, but the data maintained by the object cannot be changed after the object is created, such as string and tuple in Python, they do not support adding or deleting operations. In this chapter, we will study the variable-length object-String object in Python.

In Python, PyStringObject is an abstract and representation of string objects. PyStringObject is an object with variable-length memory, which is easy to understand, because for two different PyStringObject objects that represent "Hi" and "Python, the memory space needed to save the string content is obviously different.

However, the PyStringObject object is an Immutable object ). After a PyStringObject object is created, the internally maintained strings of the object cannot be changed. This feature enables the PyStringObject object to serve as the key value of PyDictObject, but also greatly reduces the efficiency of some string operations, such as the connection operation of multiple strings.

The Declaration of the PyStringObject object is as follows:

 
 
  1. [stringobject.h]   
  2. typedef struct {   
  3.  PyObject_VAR_HEAD  
  4.   long ob_shash;    
  5. int ob_sstate;    
  6. char ob_sval[1];} PyStringObject; 

In the definition of PyStringObject, we can see that the header of the PyStringObject object is actually a PyObject_VAR_HEAD, where an ob_size variable stores the length of variable-length memory maintained in the object. Although in the definition of PyStringObject, ob_sval is a character array.

However, ob_sval actually points to a memory segment as a character pointer, which stores the actual string maintained by this string object. Obviously, this memory segment is not just a byte. The actual length of the memory in this section) is maintained by the ob_size. This mechanism is the implementation mechanism of all objects with variable length memory in Python. For example, for the PyStringObject "Python", the value of ob_size is 6.

Like the string in C, the internally maintained string in PyStringObject must end with '\ 0' at the end, but because the actual length of the string is maintained by ob_size, therefore, the character '\ 0' may appear in the middle of the string object represented by PyStringObject, which is different from the C language because it is in C. If the character '\ 0' is encountered, the end of a string is considered. So, in fact, ob_sval points to a segment of memory with a length of ob_size + 1 byte, and must meet ob_sval [ob_size] = '\ 0 '.

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and encoding solution
  5. Analysis of Python GIL and thread security

Related Article

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.