Skip to content
New issue

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

Add __bool__ to None #56856

Closed
rhettinger opened this issue Jul 27, 2011 · 7 comments
Closed

Add __bool__ to None #56856

rhettinger opened this issue Jul 27, 2011 · 7 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@rhettinger
Copy link
Contributor

BPO 12647
Nosy @brettcannon, @rhettinger, @pitrou, @vstinner, @durban, @ericsnowcurrently
Files
  • none_bool.diff: Py3.3 patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2011-07-28.16:56:42.947>
    created_at = <Date 2011-07-27.19:26:16.149>
    labels = ['interpreter-core']
    title = 'Add __bool__ to None'
    updated_at = <Date 2011-07-28.16:56:42.946>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2011-07-28.16:56:42.946>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2011-07-28.16:56:42.947>
    closer = 'rhettinger'
    components = ['Interpreter Core']
    creation = <Date 2011-07-27.19:26:16.149>
    creator = 'rhettinger'
    dependencies = []
    files = ['22781']
    hgrepos = []
    issue_num = 12647
    keywords = ['patch']
    message_count = 7.0
    messages = ['141269', '141279', '141280', '141281', '141282', '141283', '141308']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'rhettinger', 'pitrou', 'vstinner', 'daniel.urban', 'python-dev', 'eric.snow']
    pr_nums = []
    priority = 'low'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue12647'
    versions = ['Python 3.3']

    @rhettinger
    Copy link
    Contributor Author

    Currently bool(None) returns False only because it has a special case in PyObject_IsTrue(). All other types can become False only if they implement __bool__() or __len__().

    I propose removing the special case branch and instead adding __bool__ to None. This will simplify the explanation of what bool() does to: "bool(x) always returns True unless the object defines __bool__() to return False or defines __len__() to return a non-zero value".

    The removal of the special case will slightly slow down tests for "if None", and it will slightly speed-up tests for "if x" where x is something other than True, False, or None.

    @rhettinger rhettinger self-assigned this Jul 27, 2011
    @rhettinger rhettinger added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jul 27, 2011
    @brettcannon
    Copy link
    Member

    It all sounds good to me. Less magic behind the scenes is good.

    @ericsnowcurrently
    Copy link
    Member

    And this doesn't impact "if x is None", so +1.

    @pitrou
    Copy link
    Member

    pitrou commented Jul 27, 2011

    I'm not sure why the special case shouldn't be retained. It's obviously there for speed reasons.

    @vstinner
    Copy link
    Member

    I'm not sure why the special case shouldn't be retained.

    We can keep the fast path in PyObject_IsTrue but add a __bool__ method to NoneType.

    @pitrou
    Copy link
    Member

    pitrou commented Jul 27, 2011

    > I'm not sure why the special case shouldn't be retained.

    We can keep the fast path in PyObject_IsTrue but add a __bool__ method to NoneType.

    That's what I meant.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 28, 2011

    New changeset ccce01988603 by Raymond Hettinger in branch 'default':
    bpo-12647: Add __bool__() method to the None object.
    http://hg.python.org/cpython/rev/ccce01988603

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants