How strings are stored in python memory model

2024/10/12 18:22:04

I am from c background and a beginner in python. I want to know how strings are actually stored in memory in case of python.

I did something like

s="foo"id(s)=140542718184424id(s[0])= 140542719027040
id(s[1])= 140542718832152
id(s[2])= 140542718832152

I did not understand how each character is getting stored in memory and and why id of s is not equal to id of s[0] (like it use to be in c) and why id of s1 and s2 are same?

Answer

Python has no characters. Indexing into a string creates a new string, which (like every other object) promptly vanquishes if you don't keep a reference to it around. So the id()s in your example can't be compared with each other, an object's id is only unique as long as the object lives. In particular, id(s[0]) != id(s) because the former is a new (temporary) object, and id(s[1]) == id(s[2]) because after the first operand is evaluated, the first temporary string is destroyed and the second temporary string is allocated to the previously freed memory. The latter is an implementation detail and a coincidence and cannot be relied on.

Reasoning about string memory is further complicated by implementation details like small strings (along with integers, some tuples, and more) being interned, so some_str is other_str may be true for equal strings that come from different sources (e.g. from indexing into a string with different indices).

https://en.xdnf.cn/q/69623.html

Related Q&A

The _imaging C module is not installed (on windows)

Im trying to generate some pdf with django/PIL/Imaging and everything is good until I attempt to put some images into the pdf:Exception Type: ImportError Exception Value: The _imaging C module is n…

HOW TO use fabric use with dtach,screen,is there some example

i have googled a lot,and in fabric faq also said use screen dtach with it ,but didnt find how to implement it? bellow is my wrong code,the sh will not execute as excepted it is a nohup taskdef dispatc…

Developing for the HDMI port on Linux

How would it be possible to exclusively drive the HDMI output from an application, without allowing the OS to automatically configure it for display output?For example, using the standard DVI/VGA as t…

Hive client for Python 3.x

is it possible to connect to hadoop and run hive queries using Python 3.x? I am using Python 3.4.1.I found out that it can be done as written here: https://cwiki.apache.org/confluence/display/Hive/Hiv…

How to add a function call to a list?

I have a Python code that uses the following functions:def func1(arguments a, b, c):def func2(arguments d, e, f):def func3(arguments g, h, i):Each of the above functions configures a CLI command on a p…

How to do fuzzy string search without a heavy database?

I have a mapping of catalog numbers to product names:35 cozy comforter 35 warm blanket 67 pillowand need a search that would find misspelled, mixed names like "warm cmfrter".We have code u…

Logging while nbconvert execute

I have a Jupyter notebook that needs to run from the command line. For this I have the following command:jupyter nbconvert --execute my_jupyter_notebook.ipynb --to pythonThis command creates a python s…

How to provide input for a TensorFlow DNNRegressor in Java?

I managed to write a TensorFlow python program with a DNNRegressor. I have trained the model and is able to get a prediction from the model in Python by manually created input (constant tensors). I hav…

Adding breakpoint command lists in GDB controlled from Python script

Im using Python to control GDB via batch commands. Heres how Im calling GDB:$ gdb --batch --command=cmd.gdb myprogramThe cmd.gdb listing just contains the line calling the Python scriptsource cmd.pyAnd…

Getting the maximum accuracy for a binary probabilistic classifier in scikit-learn

Is there any built-in function to get the maximum accuracy for a binary probabilistic classifier in scikit-learn?E.g. to get the maximum F1-score I do:# AUCPR precision, recall, thresholds = sklearn.m…