Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to expound Python PyString Object object

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to describe Python PyString Object objects, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

The parameters passed in by Python PyString Object must be pointers to an array of characters ending in NULL, which is not required by PyString_FromStringAndSize, because the number of characters to be copied can be determined by passing in the size parameter.

The ob_shash variable in Python PyStringObject caches the hash value of the object, which avoids recalculating the hash value of the string object every time. If a PyStringObject object has not yet been evaluated for a Hash value, the initial value of ob_shash is-1. When calculating the hash value of an object, the following algorithm is used:

[stringobject.c] static long string_hash (PyStringObject * a) {register int len; register unsigned char * p; register long x; if (a-> ob_shash! =-1) return a-> ob_shash; len = a-> ob_size; p = (unsigned char *) a-> ob_sval; x = * p = 0) x = (1000003roomx) ^ * pendant; x ^ = a-> ob_size If (x =-1) x =-2; a-> ob_shash = x; return x;}

The ob_sstate variable of the Python PyStringObject object whether the object is marked by Intern. The Intern mechanism of PyStringObject will be described in detail later. Let's take a look at the corresponding type object of PyStringObject:

[stringobject.c] PyTypeObject PyString_Type = {PyObject_HEAD_INIT (& PyType_Type) 0, str, sizeof (PyStringObject), sizeof (char),. (reprfunc) string_repr, / * tp_repr * / & string_as_number, / * tp_as_number * / & string_as_sequence, / * tp_as_sequence * / & string_as_mapping, / * tp_as_mapping * / (hashfunc) string_hash, / * tp_hash * / 0 / * tp_call * /. String_new, / * tp_new * / PyObject_Del, / * tp_free * /}

As you can see, in the type object of Python PyString Object, tp_itemsize is set to sizeof (char), which is one byte. For any kind of variable length object in Python, the tp_itemsize field must be set. Tp_itemsize indicates the unit length of the element saved by the variable length object, which refers to the length of an object in memory. This tp_itemsize and ob_size together determine the total amount of memory that should be applied for.

The above is how to describe Python PyString Object objects. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report