| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Expand Up | @@ -2037,12 +2037,15 @@ Basic customization | |||||||||||||||||||||||
| .. index:: single: __len__() (mapping object method) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Called to implement truth value testing and the built-in operation | ||||||||||||||||||||||||
| ``bool()``; should return ``False`` or ``True``. When this method is not | ||||||||||||||||||||||||
| :func:`bool`; should return ``False`` or ``True``. When this method is not | ||||||||||||||||||||||||
| defined, :meth:`~object.__len__` is called, if it is defined, and the object is | ||||||||||||||||||||||||
| considered true if its result is nonzero. If a class defines neither | ||||||||||||||||||||||||
| :meth:`!__len__` nor :meth:`!__bool__`, all its instances are considered | ||||||||||||||||||||||||
| true. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Two successive calls to :meth:`!__bool__` on the same object must | ||||||||||||||||||||||||
| return same value. | ||||||||||||||||||||||||
|
Comment thread
Comment on lines
+2046
to
+2047
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI'm unsure if this needs to be explicitly documented as the behavior is dependent on the user's choice of implementation.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Rather on bugs in the user code, if someone will implement __bool__(), doing crazy things (see issue). Added docs say that implementation may assume certain behaviour from the user code, just as for the __hash__() method (same hash value for equal objects - the invariant, which we also can't enforce, user code might break this). PS: I think that part of discussion rather belongs to the issue thread, which has some other arguments on why we want document this. See e.g. this.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityShould the note warn against side effects in general (like I/O), not just mutation?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Side effects, including in fact object mutation - are fine, unless they break idempotence. But I think we don't loose something practically relevant if just forbid mutation of any objects in __bool__() (a shortened version of @ncoghlan suggestion):
Suggested change
Sorry, something went wrong.
All reactions
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| .. _attribute-access: | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Expand Down Expand Up | @@ -2947,6 +2950,9 @@ through the object's keys; for sequences, it should iterate through the values. | |||||||||||||||||||||||
| :meth:`~object.__bool__` method and whose :meth:`!__len__` method returns zero is | ||||||||||||||||||||||||
| considered to be false in a Boolean context. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Two successive calls to :meth:`!__len__` on the same object must | ||||||||||||||||||||||||
| return same value. | ||||||||||||||||||||||||
|
Comment thread
Comment on lines
+2953
to
+2954
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityHere is a typo (__len__ -> __bool__). But if we are going with this lengthly wording - I think it's better just point to the __bool__ docs.
Sorry, something went wrong.
All reactions
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| .. impl-detail:: | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| In CPython, the length is required to be at most :data:`sys.maxsize`. | ||||||||||||||||||||||||
| Expand Down | ||||||||||||||||||||||||
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityNot if the object is mutated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityBut that means - some other method was called on the object in between. Thus, calls to __bool__ weren't actually successive.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThe object could have been mutated by another thread between the two calls...
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality... that calls some other object's method
:)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityHmm, how about this: "The __bool__ method can't mutate any objects."? (Ditto for __len__.)
This probably is a more strong requirement than actually need in current optimizations, but I doubt it blocks something useful.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.