2
class A:
     def self_method(self):
         pass

     @staticmethod
     def static_method():
         pass


a1, a2 = A(), A()

print(a1.self_method == a2.self_method,
      id(a1.self_method) == id(a2.self_method),
      a1.self_method is a2.self_method)

print(a1.static_method == a2.static_method,
      id(a1.static_method) == id(a2.static_method),
      a1.static_method is a2.static_method)

Output:

(False, True, False)
(True, True, True)

Why is id(a1.self_method) == id(a2.self_method) True, but a1.self_method is a2.self_method is False? What does it mean? Does python recreate this method/s for every object or not?

ruohola
  • 21,987
  • 6
  • 62
  • 97
droso
  • 31
  • 2
  • 2
    @DeepSpace How does your suggested duplicate address the difference between `id(...) == id(...)` and `is` here? One of the answers even says "`a is b` is the same as `id(a) == id(b)`", which is apparently not true in this case. – sepp2k May 29 '19 at 15:19
  • Are you using Python 2 or Python 3? The omission of `object` in the class suggests Python 3, but the output of tuples from the prints suggests Python 2. – wim May 29 '19 at 15:42

0 Answers0