Home > other >  The python string address problems
The python string address problems

Time:11-16

Python is a small white one asked a question that I really care, I check the string in the python3.8.2 code is the result of this
> Id (' CPCCP ')
4300388336
> Id (' oee8ed.)
4300388336
But not so in 3.7
Id (' CPCCP ')
4368612352
Id (' oee8ed ')
4370164792
Why different string address in 3.8, in 3.7 the inside address is different, do you have a great god can help to solve it, as if no one ask this question everywhere, I'm here to see,
But also seems to have the same point, the following & gt;> A='apple'
> B='basket'
> A + b is' applebasket # a + b or a + 'basket' is a false
: 1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
> Id (a + b)
4300389360
> Id # (" apple "+ b) output address is the same
4300389360
> Id (' apple '+' basket ')
4300389360
=================================================
A='apple' # python3.7
B='basket'
A + b is' applebasket '
False
Id (a + b)
4369049264 # is different from the address below
Id (a + 'basket')
4366530480


As above, if you can solve many thanks

CodePudding user response:

First of all, python id function and take the address of the C language & amp; Similar, but also a little different:
C & amp; Operation is real to score a memory address or the address of the virtual memory page, returns the address values, there is a real address,
And python id () function is assigned to each object only a fixed integer values,

Look at the official document:
"Id (object) :
Return the "identity" of an object. This is an integer which is guaranteed to be unique and constant for This object during its lifetime. Two objects with non - overlapping lifetimes have the same id () value. "
Quote from python.org

Is not get an address, but a unique fixed integer values.
And: no overlap of the life cycle of two different objects (two objects with non - overlapping lifetimes)
May have the same id value (have the same id () value)

1.
That explains your first question:
Under the command line, id (' abcd ') and then run id (' 123 ') may have the same value, because the first value of the life cycle has ended, and then start the second value, so can the same id,
Can image understanding is: a certain position on the blackboard to write a line, erase and then write another word in the same position, position is different, the same word is a truth,

As for the version problem, 3.8, 3.7, just different version compilation system differences, does not affect the definition of id () function,

2.
Python has a important concept: string is fixed, can not be changed, this and C language is different,
So, python, if the same two strings, they can have the same id (python check first to have the same string, use off-the-shelf), can also be different
And different strings, they must have different id
> X='abcd'
> Y='123'
> Z='abcd'
> Id (x)
69937312
> Id (z)
69937312
> Id (y)
69937408


This explains to your second question,
And, of course, different versions, compile may be little difference,

Personal humble opinion, reference
  • Related