0

I'm using the pow function and found out I had a bug in my code because == and is were not having the same behavior.

Here goes an example: pow(3, 47159012670, 47159012671) == 1 returns True but pow(3, 47159012670, 47159012671) is 1 returns False.

I'd like to know what is it that I'm not getting.

leti
  • 1
  • `1` uses the cached `1` object; `pow(...)` creates a new `1`. – Amadan Apr 18 '19 at 05:33
  • People too commonly treat CPython's small integer caching as if it's an actual guarantee. As demonstrated here, it's not a guarantee, even in CPython. – user2357112 Apr 18 '19 at 05:35

1 Answers1

0

The difference between is and == is:

a1 is a2 # return True

only when they share the same location or id in memory, that means when id(a1) and id(a2) are the same. I hope this will help more.

Tigran Fahradyan
  • 399
  • 1
  • 4
  • 12
  • At least explain when somebody down votes, may be there is something for me to learn from my mistakes)) – Tigran Fahradyan Apr 18 '19 at 05:38
  • Thinking about this in terms of memory locations is like trying to distinguish whether two humans are the same human by seeing whether they're sitting in the same chair. Also, the Python language is not defined in terms of memory locations, and some Python implementations (e.g. PyPy) will return `True` for `is` even if two (or more!) memory locations happen to be used for the arguments. – user2357112 Apr 18 '19 at 05:40
  • I understand more now. Thanks – Tigran Fahradyan Apr 18 '19 at 05:44