0

My understanding of "is" is that it checks if the two operands point to the same structure.
I am unsure why this is true

x = 1
y = 1
y is x

and why this is false

l1 = [1,2,3]
l2 = [1,2,3]
l1 is l2

Similarly, why is the first not false as well? Is it saying that when I initialize x, it creates the object with int value "1", and from now on, whenever I initialize variables to have "1", it will point to the first object.

If so, I am curious why its not the same with the second example.

OneGapLater
  • 109
  • 1
  • In the link, what does the top answer mean by "only catches small integers"? Is it saying that for my second example, the reason it's false is because of how Python is set up? So the first example gives True since Python was *designed* to be true for "small" integers? – OneGapLater Jul 10 '18 at 14:35
  • And essentially, doing l1 = [1,2,3] l2 = [1,2,3] will make 2 separate ((not sure what word to use since I'm new to Python))'s? whereas x = 1 y = 1 will *not* make ((not sure what word to use since I'm new to Python))'s? – OneGapLater Jul 10 '18 at 14:36
  • Caches, not catches. – Sergio Tulentsev Jul 10 '18 at 14:37

0 Answers0