аватар question@mail.ru · 01.01.1970 03:00

Is there a strict comparison operator in Python like ===?

Just a conventional comparison through == does not work as it should be:

   & gt; & gt;   0  ==  false    true    
аватар answer@mail.ru · 01.01.1970 03:00

If the essence of the issue is compared to meanings and types, then you can do it:

   def   strict_eq  ( obj1, obj2 ):   f   type  (obj1)! =  type  (Obj2):   retu   false    reta> obj1 == obj2    
  in [ 4 ]: strict_eq ( 0 ,  false ) out [ 4 ]:  false       ps can begun to go Even further and for numerical types, compare the numbers to certain accuracy in order to avoid known problems with the floating point: 

  in [ 5 ]:  0.1  +  0.2  ==  0.3  out [ 5 ]:  false   

a strict comparison function with a certain accuracy:

   from  numbers  import  number  def   strict_eq  ( OBJ1, OBJ1, OBJ2, Epsilon =  1e-7  ):   if   type  (obj1)! =  type  (Obj2):   retu   false    if   isinstance  (Obj1, Number):   retu   abs  (obj1 - obj2) & lt; Epsilon  retu  obj1 == obj2 

  in [ 10 ]: strict_eq ( 0.1  +  0.2 ,  0.3 ) out [ 10 ]:  true                                        

Latest

Similar