In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what is the use of is and = = in Python", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is the use of is and = = in Python".
1. = = is to compare whether the contents of two objects are equal
That is, whether the "values" of two objects are equal, regardless of whether they have the same reference address in memory.
/ / the address is the same, the value is the same. So = = holds. St1= 'aaaaa'st2 =' bbbbb'st3 = 'bbbbb'st4 = st3print (st1==st2,st2==st3,st3==st4) # False True Trueprint (id (st2) = = id (st3), st2==st3) # True True / / reference address is different, but as long as the value is the same, = = is true. > > val1 = 2000 > val2 = 2001 > > val3= val1 + 1 > print (id (val3) = = id (val2), val3==val2) False True / / compare for class instances class Student (object): def _ init__ (self,name,age): self.name = name self.age = age def run (self): print ("can run") stu1 = Student ("tom", 19) stu2 = Student ("tom", 19) stu3 = stu2print (id (stu1) = id (stu2) Stu1 = = stu2) # False False
Note: the values of stu1 and stu2 are different here, although the initialization creates the object in the same format.
Print (id (stu2) = = id (stu3), stu2 = = stu3) # True True
2. Is compares whether two instance objects are exactly the same.
Whether they are the same object and whether they occupy the same memory address. That is, is compares two conditions: 1. It's the same. two。 Same address in memory
/ / if the premise of is is the same, the memory address is the same st1 = 'aaaaa'st2 =' bbbbb'st3 = 'bbbbb'st4 = st3print (st1 is st2, st2 is st3,st3 is st4) # False True Trueprint (id (st1), id (st2), id (st3), id (st4)) # 2439382008080 2439382008192 2439382008192 / / the memory address must be the same. > a = 1 > a = 1000 > > b = 1000 > print (id (a), id (b)) 2625727620144262572761948 > print (an is b) False > print (a = = b) True > / the comparison of class instances should also have the same memory address. Class Student (object): def _ init__ (self,name,age): self.name = name self.age = age def run (self): print ("can run") stu1 = Student ("tom", 19) stu2 = Student ("tom", 19) stu3 = stu2print (id (stu1), id (stu2), id (stu3)) print (stu1 is stu2,stu2 is stu3) = = 209192 26776 209192 2655888 2091922655888False True 3, use is Note python for small integers using object pool storage problem
For example, in python command-line mode: why is the result different between the same value of aforme b and c penny d?
> a = 1000 > b = 1000 > > an is bFalse > c = 10 > > d = 10 > c is dTrue
Note: because python creates a copy of small integers directly in memory and will not be recycled, all created small integer variables can be referenced directly from the object pool.
Note, however, that Python caches only smaller integer objects (in the range of [- 5,256]), not all integer objects.
This means that the values of variables created in this [- 5256] range will only be valid if they are compared with is.
> e, d, f, g =-5,-5,-6,-6 > e is dTrue > f is g # beyond the range of-5 does not hold False >
Note: the above use of the python small integer object pool can only be executed on the command line, but in Pycharm or saved as a file, the results are different because the interpreter has made some optimizations. The following use pycharm, even if the integer is more than 256, the use of is is true.
Fourth, use is to pay attention to python's intern mechanism storage of strings.
Note: when creating two variables with the same content in python (variable names are not the same), two memory addresses are usually assigned to the two variables in memory. That is, although the contents of the two variables are the same, the reference addresses of the variables are different. So the use of = = for the two variables is more valid, but the use of is is not.
But there are two surprises in python:
When using the python command line, python creates a pool of small integer objects for integers in the range of [- 5256]. Once these objects are created, they are not recycled, and all newly created integers in this range refer to him directly. Therefore, as a result, as long as the values of the integers in the interval [- 5256] are the same, the reference addresses are also the same. Integers outside this range also follow the creation of a new variable to assign an address.
Although the string object in python is also an immutable object, python has an intern mechanism, which simply maintains a dictionary that maintains the created string (key) and the address of its string object (value). Every time a string object is created, it will be compared with this dictionary. It is equivalent to that python also uses the object pool principle for strings.
(but note: if the string (contains spaces), it cannot be modified, the intern mechanism is not enabled, and objects are not shared. For example, "a b" and "a b" can only be used on the command line in a form that is does not hold. Using pycharm is also True, because it is optimized)
> a = 'abc' # does not have two variables with the same space content. In command line mode, is result True > b =' abc' > a = = bTrue > an is bTrue > c ='ab'# has two variables with the same space content. In command line mode, is result false > > d ='ab'> > c = = dTrue > c is dFalse
Summary:
So if you create multiple variables in python (different variable names, in addition to variables that are not created through variable references), the reference addresses of these variables are different. So if you use is to compare these variables, it is the result of False. Except for small integers and strings. If the variable is created by reference, you can refer to the
The principle of replication and Storage of variable references in memory blog: references to python variables and how to store them in the underlying layer
> > ls1 = [1pg2 ls1 3] > ls = = ls1True > ls is ls1False > T1 = (1p2pje 3) # # values are the same, but there are two variables and two addresses are allocated in memory. > T2 = (1Power2 is t2False 3) > > T1 = = t2True > T1 > > D1 = {"1": 2, "3": # # values are the same, but there are two variables and two addresses are assigned in memory. > D2 = {"1": 2, "3": > > D1 = = d2True > > D1 is d2False > > id (D1), id (D2) (5425744, 4537872) > st1 = 'abc' # Note here st1 and st2, the value is the same, the memory address is the same. Because of the intern mechanism of strings. > st2 = 'abc' > st1 = = st2True > > st1 is st2True > F1 = 3.14 # although the value is the same, but there are two variables and two addresses are assigned in memory. > > f2 = 3.14 > > F1 = = f2True > > F1 is f2False > > a = 1000 # the values beyond the [- 5256] range are the same, but there are two variables and two addresses are allocated in memory. > b = 1000 > an is b, a = = b (False, True) > a = 1 # value is within the pool of small integer objects, so the value is the same and the memory address is the same. > b = 1 > an is b, a = = b (True, True) > 5.python comparison of None values: use is > > a = "" > > an is NoneFalse > b = "aa" > b is NoneFalse > b is not NoneTrue is all the content of this article entitled "what is the use of is and = = in Python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.