From 1022c99b871af828693a8b9b58cd47f14e16deec Mon Sep 17 00:00:00 2001 From: Gourav Chawla Date: Sun, 15 May 2016 20:32:00 +0530 Subject: [PATCH 1/3] Added Python-Function-ID.md For issue #819 --- Python-Function-ID.md | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Python-Function-ID.md diff --git a/Python-Function-ID.md b/Python-Function-ID.md new file mode 100644 index 000000000..20c449741 --- /dev/null +++ b/Python-Function-ID.md @@ -0,0 +1,47 @@ +# Python id(object) + +`id()` is a built-in function in Python 3, which returns the *identity* of an object. The *identity* is a unique integer for that object during its lifetime. In CPython implementation, this is the address of the object in memory which is not necessarily true for other python implementations. + +## Argument + +### object + +The `object` argument can typically be a `int`,`float`, `str`,`list`, `dict`, `tuple` etc. + +## Return Value + +The return value would be a unique and constant `integer`. + + +## Code Sample + +```python +a = 2 +print(id(a)) #=> 140454723286976 (Values returned by id() might be different for different users) + +b = 3 +print(id(b)) #=> 140454723287008 + +c = 2 +print(id(c)) #=> 140454723286976 (This is same as id(a) since they both contain the same value and hence have same memory address) + +print(id(a) == id(b)) #=> False (since a and b have different values stored in them) +print(id(a) == id(c)) #=> True (since a and c have same values stored in them) + +d = 1.1 +e = 1.1 +print(id(d) == id(e)) #=> True (since d and e have same values stored in them) + +str1 = 'hello' +str2 = 'hello' +print(id(str1) == id(str2)) #=> True (since str1 and str2 have same values stored in them) + +# For complex objects like lists, tuples, dictionaries etc. id() would give a unique integer even if the content of those containers is same. +tup1 = (1,1) +tup2 = (1,1) +print(id(tup1) == id(tup2)) #=> False +``` + +:rocket: [REPL It!](https://repl.it/CQw7/1) + +[Official Docs](https://docs.python.org/3/library/functions.html#id) From 1f4ebd85e04c33cda7204d0d58cb69d6944516b4 Mon Sep 17 00:00:00 2001 From: Gourav Chawla Date: Mon, 16 May 2016 08:09:37 +0530 Subject: [PATCH 2/3] Removed duplicate line converted object heading to h4 and removed duplicate line about return value. --- Python-Function-ID.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Python-Function-ID.md b/Python-Function-ID.md index 20c449741..2b4cb1ead 100644 --- a/Python-Function-ID.md +++ b/Python-Function-ID.md @@ -4,15 +4,10 @@ ## Argument -### object +#### object The `object` argument can typically be a `int`,`float`, `str`,`list`, `dict`, `tuple` etc. -## Return Value - -The return value would be a unique and constant `integer`. - - ## Code Sample ```python From 4e3df1301ec3839e3e2fa0826dd8ab58bf05bc64 Mon Sep 17 00:00:00 2001 From: Gourav Chawla Date: Tue, 17 May 2016 09:24:04 +0530 Subject: [PATCH 3/3] Removed different python implementation details The different python implementation details are confusing and could be ignored. The id() function is not used that much so omitting the statement makes more sense. --- Python-Function-ID.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python-Function-ID.md b/Python-Function-ID.md index 2b4cb1ead..56accffb9 100644 --- a/Python-Function-ID.md +++ b/Python-Function-ID.md @@ -1,6 +1,6 @@ # Python id(object) -`id()` is a built-in function in Python 3, which returns the *identity* of an object. The *identity* is a unique integer for that object during its lifetime. In CPython implementation, this is the address of the object in memory which is not necessarily true for other python implementations. +`id()` is a built-in function in Python 3, which returns the *identity* of an object. The *identity* is a unique integer for that object during its lifetime. This is also the address of the object in memory. ## Argument