The string obtained by python input cannot be judged by is

test = input("TT >>").strip()
print(type(test))
print(type("TT"))
print(id(test))
print(id("TT"))

if test is "TT":
    print("Yes")

after the above code runs, the result is

TT >>TT
<class "str">
<class "str">
4364442848
4364036392

I wrote the string "TT"," in the input. What I get here is the string "TT",python. The string type is immutable. Why is the id I get here different?

Test code on the other side

tt2 = "TT"
tt3 = "TT"
print(id(tt2))
print(id(tt3))

get the result:

4314464360
4314464360

the id here is the same. Is the return value obtained by the input function different from the string assigned directly like this?

the environment runs python3 under MAC

Mar.04,2021

according to the python official documentation, the id () function is interpreted as follows

In [1]: id?
Signature: id(obj, /)
Docstring:
Return the identity of an object.

This is guaranteed to be unique among simultaneously existing objects.
(CPython uses the object's memory address.)
Type:      builtin_function_or_method

returns a dynamically created string with the input () function, whose memory address is not predictable.
while tt2 = 'TT'; tt3 =' TT'; , the python interpreter saves only the first 'TT' string, and the second' TT' is directed to the previous one. So their id () results are the same. Changing tt2 or tt3 will result in a new string without changing the 'TT' content.


this question seems simple, but it touches the bottom of the computer!

according to the first chapter of in-depth understanding of computer Systems, information is bit + context .

so, let's take a look at how Python defines the id () function:

>>>help(id)
Help on built-in function id in module builtins:

id(obj, /)
    Return the identity of an object.

    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)
(END)

it is obvious that the, id (object) function returns the address of the object object in memory.
however, according to the definition of information in in-depth understanding of computer Systems, you will realize the frivolous tonality of python documents. There is no mention of context , no mention of object life cycle , which is not rigorous. Because the important context relationship is not mentioned.

for a better understanding, especially the life cycle, that is, contextual relationships ,

I made two scripts:

test1.py is as follows:

print(__name__)
test1 = input('ttt>>>').strip()
print(test1)
print(id(test1))


test2 = 'TT'
print(id(test2))

test2.py is as follows:

test3 = 'TT'
print(__name__)
print(id(test3))

run the first script, showing that the main program is _ _ main__ . As a result, you will find that the script is the same as the subject, and the two id are different.
run the second script, which shows that the main program is _ _ main__ . As a result, you will find that the id here is also different from the first.

that is, the corresponding memory address of the object is different because the context , or the life cycle of the program is different.

be more straightforward , you run the first script many times, and each time id displays almost is different. The same is true of
running the second script.
as to why I use the word almost , I'll leave it to you to look it up.


>>> test = input('TT>>')
TT>>'TT'
>>> test2 = 'TT'
>>> id(test)
4401760416
>>> id(test2)
4401760416

the above is the result of execution in the Python2 environment. Both id are the same.
so what the subject really needs to know is the difference between input in python2 and python3.


How to choose

= = and is?

The

= = operator compares the values of two objects, and you can use the _ _ eq__ magic method overload to achieve a custom comparison.
is compares the id identities of two objects and cannot be overloaded. It is usually used to compare variables directly with singleton values, such as is None.

Why is id the same sometimes?

first of all, id is not the same just because it is an immutable type.
Cpython has a detail optimization called resident (interning), which optimizes strings and small integers to share the same reference.
note, however, that this optimization does not apply to immutable types, nor does it apply to all strings and integers, as shown in the source code implementation.

Menu