The title of this article is divided into two parts :(1)Python Whether copy string is supported in ?(2) If not , Why not support ?
Please take a few minutes to think about , Think about it , Remember your answer , Then look down .
Let's make an agreement ( Voluntary compliance ): If you see the last , You overturned the present answer , Build a new understanding , This shows that what I wrote is useful , Please feel free to appreciate , Or share this article with others Python Little buddy .
1. What is a copy string ?
First , Everyone must be right “ Copy ” There is a consensus on this concept . Copy , Also called copy , The English word is copy, The specific meaning is “ The act of making one or more copies of something in some way ”( The explanation comes from Wikipedia ). The result of replication is , There are many things that are very similar but independent of each other ( copy ), for instance , You have a document X, Then make a copy and rename it Y, The two are independent of each other , If you delete one of them , The other will not be deleted together .
This word is used in Python in , We want to express the same meaning , That is, the copy behavior will produce new independent objects , It is very similar to the original object , But there is no direct relationship between the life cycle of the two . Let's take a list as an example :
list1 = [1,2]
id(list1)
>>> 1981119454856
list2 = list1.copy()
print(list1 == list2)
>>> True
id(list2)
>>> 1981116983752
Copy code
In the above example , list list2 yes list1 Copy of , Both are literally equal , But the memory address ( namely id ) It's not equal , Are two independent objects . If the string can do the same , Then let's say , Strings can be copied , otherwise , We say that strings cannot be copied .
2. How can I copy strings ?
With the above concepts and examples , Please think first. , How do you copy strings ?( Pause , reflection 3 minute )
Okay , Let's take a look at the following methods :
s0 = "Python Programming learning circle "
s1 = s0
s2 = str(s0)
s3 = s0[:]
s4 = s0 + ''
s5 = '%s' % s0
s6 = s0 * 1
s7 = "".join(s0)
import copy
s8 = copy.copy(s0)
Copy code
Do you think of the above replication methods 8 In two ways ? that , If you put s0 to s8 Of id Print out , What will follow s0 It's different ?
The answer is , Their memory addresses id Exactly the same , in other words , A meal operation as fierce as a tiger , The result is always a single string , No new strings have been copied at all !
A knowing smile , This is not because of the string Intern Mechanism , A short string can only exist in memory
But please don't be happy too soon , You can take s0 Change to a super long string , for example :
s0 = "Python Programming learning circle , Is learning Python, Its wechat official account is also called Python Programming learning circle , Welcome to pay attention "
then , Repeat the above operation . Final , You'll find that ,s0 To s8 Of id Or exactly the same .
Are you surprised ? new s0 Obviously, it has exceeded Intern The length of the mechanism , Why not generate a new string ?
First , Please believe in , beyond Intern The string of the mechanism can exist in multiple copies , That is, you can create multiple string objects with identical values , Because string objects are not necessarily unique in memory :
s9 = "Python Programming learning circle , Is learning Python, Its wechat official account is also called Python Programming learning circle , Welcome to pay attention "
print(id(s0) == id(s9))
>>> False
Copy code
The above example shows , You can create multiple identical string objects , But this method is different from the one listed above 8 Species difference , Because it is independent of s0 The operation of , It is not a copy operation . In theory ,Python Can provide a method , Achieve the result of copying a new copy . The problem now is precisely : Why are multiple equal string objects allowed , But it can't be created by copying ?
**
When looking up information , I find that many articles on the Internet are helpful for “ Immutable object ” There are misunderstandings , These people don't know Intern The existence of mechanisms , Mistakenly thinking that there can only be one string object in memory , And then mistakenly think that immutable objects are objects with only one copy in memory . therefore , It is easy to infer wrong conclusions from these articles : Because strings are immutable , So the string does not support copying .
in fact , Between immutable objects and copy operations , There is no necessary strong correlation . It must be for some other reason , Designers have imposed such restrictions on immutable objects , What is the reason ?
On zhihu , A keen classmate raised my question “Python How to copy a value or string in ?”, It's a pity that 4 answer , And they didn't answer the question .Stackoverflow There also happens to be a problem “How can I copy a Python string?”, Similarly, not many people have noticed , Only 5 answer , Fortunately, the highest answer mentioned a point , That is, it can speed up the search of the dictionary .
However , What he said is not reliable . The dictionary requires that the key value be a hashable object , However, the hash value of the string is calculated according to the literal value , So for multiple equal string objects , The hash value is actually the same , It has no effect on calculation and search .
w1 = "Python Programming learning circle , Is learning Python, Its wechat official account is also called Python Programming learning circle , Welcome to pay attention "
w2 = "Python Programming learning circle , Is learning Python, Its wechat official account is also called Python Programming learning circle , Welcome to pay attention "
print(w1 == w2)
>>> True
print(id(w1) == id(w2))
>>> False
print(hash(w1) == hash(w2))
>>> True
Copy code
Keep checking the data , Finally in the 《 smooth Python》 Found a clear explanation :
These details are CPython Shortcuts and optimization measures taken by core developers , For users of this language, there is no need to know , And those details are important to others Python Implementation may not work ,CPython Future versions may not use .
This book 《 smooth Python》 It is one of the first choice for advanced bibliography , I have read some chapters , I didn't expect that in an inconspicuous section , author “ Be surprised to find ” The non replicability of tuples , Before that , He thought himself “ Know everything about tuples ”, Ha ha ha .
although , I guess the reason is to save memory and improve speed , But seeing this clear explanation , Know it's just CPython Of the interpreter “ Jakob the Liar ”, And it may not be used in future versions , I was particularly surprised .
It confirms my guess , meanwhile , It also provides more than expected information : Other Python The interpreter may support copying immutable objects , at present CPython It was a compromise , In the future, it is possible to restore the copy operation of immutable objects !
Back to the two questions at the beginning of the article , The answer we get is :Python By itself, it does not restrict the copying of strings , Only the current version CPython Optimized , That led to this “ Jakob the Liar ”, The reason why it does so is to Intern Mechanism to complement , Try to make all string objects have only one copy in memory , To save memory .
CPython Yes, it is C The realization of language Python Interpreter , It's official. 、 The most widely used interpreter . Except for it , It's also useful Java Realized Jython Interpreter 、 use .NET Realized IronPython Interpreter 、 use Python Realized PyPy Interpreter , wait . How do other interpreters cope with string copying ? alas , knowledge has no limit , I have little talent and learning and have not dabbled in , Let's put aside our doubts .
here , I just want to make an aside ,Python The most widely criticized is GIL( Global interpreter lock ), This causes it to not support true multithreading , Become a lot of people blame Python The culprit of slowness . however , The question is CPython The interpreter brings , And like Jython The interpreter does not have this problem .
The above is all the content shared this time , Want to know more python Welcome to official account :Python Programming learning circle , send out “J” Free access to , Daily dry goods sharing