This repository has been archived by the owner on Apr 1, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 305
Article: Python Function ID #962
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -#### object
+### object |
||
|
||
The `object` argument can typically be a `int`,`float`, `str`,`list`, `dict`, `tuple` etc. | ||
|
||
## Code Sample | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -## Code Sample
+### 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A beginner would want to know what you mean by CPython implementation, so better link to the GitHub mirror of CPython interpreter, or some article that talks about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that CPython is not a perform-ant implementation. PyPy and Pyston would probably handle performance better by moving around objects in memory.
But reading it, it feels like that
id()
need to be same, is not part of the spec. Is that so? I am not fully aware of this.