+ 11

how the behaviour of ( is ) operator related to corelated data type

t1=(1,2) t2=(1,2) print(t1 is t2)#True #here tuple is immutable data type so object reusability is there but r1=range(10) r2=range(10) print(r1 is r2) #False #but here range data type is also immutable so how it is False why object reusability concept is not there

8th Jan 2020, 3:29 AM
Saroj Patel
Saroj Patel - avatar
3 Respuestas
+ 9
Thanks HonFu and Tibor Santa
9th Jan 2020, 6:17 PM
Saroj Patel
Saroj Patel - avatar
+ 5
The specification says that mutable objects have to be separate objects, but immutable objects *can* fall together as one. So it is not prescribed how immutables behave in this respect, because it doesn't matter. Each implementation of Python can do it differently, and even in one implementation and one type, sometimes the case occurs that if you create the same string with one method twice, they fall together, but if you create it with another method, you get two separate objects.
8th Jan 2020, 9:58 AM
HonFu
HonFu - avatar
+ 3
The 'is' keyword tells you whether two objects occupy the same memory location. The actual implementation of memory management could depend on the particular python interpreter (cpython, ironpython, pypy...) https://stackoverflow.com/questions/2987958/how-is-the-is-keyword-implemented-in-python
8th Jan 2020, 4:02 AM
Tibor Santa
Tibor Santa - avatar