0
a = 5

def fun(b):
  global a
  a = 5
  print(a, b) # 5, 5 
  print (a is b) # True

def foo(b):
  global a
  a = 6
  print(a, b) # 6, 5 
  print (a is b) # False

fun(a)
foo(a)

So, I'm learning python and was playing around with identity/references. As per this link, identity of object never changes once it is created.

Why does the identity change when assigning a different value to variable a that is already created? Why assignment has different behavior in the two functions?

  • 2
    ``a`` is not the **object**, it's only a name that points to an object. The actual objects here are ``5`` and ``6``. – Mike Scotty Nov 12 '21 at 10:22
  • 3
    besides small integers are usually cached so `is` will work differently there. Besides integers are immutable... well... I wouldn't worry about such things... `is` operator is pretty much useless in normal python coding – Jean-François Fabre Nov 12 '21 at 10:23

0 Answers0