- Learn Python in 7 Days
- Mohit Bhaskar N. Das
- 269字
- 2021-07-09 20:40:27
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.
- 黑客攻防從入門到精通(實戰秘笈版)
- FuelPHP Application Development Blueprints
- Spring Boot開發與測試實戰
- JavaScript 從入門到項目實踐(超值版)
- 構建移動網站與APP:HTML 5移動開發入門與實戰(跨平臺移動開發叢書)
- HTML5 Mobile Development Cookbook
- 前端架構:從入門到微前端
- Bootstrap Essentials
- Banana Pi Cookbook
- Android開發:從0到1 (清華開發者書庫)
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- Webpack實戰:入門、進階與調優
- Visual Basic程序設計上機實驗教程
- OpenStack Networking Essentials
- Python Machine Learning Cookbook