1

I thought every object take an id based on key.

>>> a = 10
>>> b = 20
>>> id(a)
1876869280
>>> id(b)
1876869440
>>> a,b=b,a
>>> id(a)
1876869440
>>> id(b)
1876869280

When swap the variables, their ids are swapped too. If every object has a unique id, then why ids are swapped? I thought id(a) and id(b) will be same after swap.

2

1 Answer 1

6

If every object has a unique id, then why ids are swapped?

Because you swapped objects. The objects aren't a and b, they're 10 and 20. a and b are just names that the code uses to refer to those objects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.