Advertisement
naren_paste

is_is not

Aug 19th, 2023
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | Source Code | 0 0
  1. # Checking for None:
  2. result = some_function()
  3. if result is None:
  4.     print("No result")
  5. else:
  6.     print("Result available")
  7.  
  8. # Comparing Singleton Objects:
  9. status = True
  10. if status is True:
  11.     print("Status is True")
  12.  
  13. # Caching Immutable Objects:
  14. a = "hello"
  15. b = "hello"
  16. if a is b:
  17.     print("Same object")
  18.  
  19. # Comparing Custom Objects:
  20. class MyClass:
  21.     pass
  22.  
  23. obj1 = MyClass()
  24. obj2 = obj1
  25. obj3 = MyClass()
  26.  
  27. if obj1 is obj2:
  28.     print("obj1 and obj2 refer to the same instance")
  29. if obj1 is not obj3:
  30.     print("obj1 and obj3 are different instances")
  31.  
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement