官术网_书友最值得收藏!

Identity operators

Given in the table are the two identity operators:

The following screenshot shows that the value can be same but id can be different. It returns True if id is the same:

Here, as both x and y contain the same value (10), id() returns the same value for both. However, when x and y are provided with the same list, id() returns different values. This could be attributed to the fact that when x and y have 10 assigned to them, it essentially means that both are pointing to the same memory address, which has a value of 10, while in the case of a list, this could be different. Why different? That is because lists are immutable, which means they can't be changed. So, when we assign the same list to y, it means that a new memory address is blocked again for a new list.

The id() function returns the identity of an object. This is an integer (or long integer), which is guaranteed to be unique and constant for this object during its lifetime. It is similar to memory addresses in the C language.

However, there is a small twist in our id() function. Python supports same memory allocation for integers only up to 256. It will be clearer with the following screenshot:

Here, as you can see, once we pass values above 256 for x and y, both are given different memory allocations by the interpreter, but if the values are 256, then the same memory allocation is provided for both x and y.

主站蜘蛛池模板: 久治县| 兴国县| 开平市| 台南市| 青川县| 鄂伦春自治旗| 许昌市| 张家川| 金堂县| 哈巴河县| 津南区| 松溪县| 九龙县| 民乐县| 临汾市| 张家港市| 信丰县| 乌兰浩特市| 三门县| 余姚市| 稷山县| 望江县| 勃利县| 准格尔旗| 勃利县| 黄陵县| 疏勒县| 朝阳区| 耿马| 龙里县| 普定县| 阳春市| 通河县| 锡林浩特市| 镇沅| 遂溪县| 集贤县| 比如县| 肇东市| 勐海县| 绵竹市|