Skip to main content
edited body
Source Link
Cireo
  • 4.5k
  • 2
  • 22
  • 27

No, but...

You cannot override the is, and, or or operators.

Defining __bool__ allows you to write statements like

class Node:
    def __init__(self, val):
        self.val = val

    def __bool__(self):
        return self.val is not None   # <--- added "return"

for val in (0, 1, True, None):
    n = Node(val)
    # These three are equivalent
    if n:
        assert xn.__bool__()
        assert n.val is not None
    # These three are equivalent
    else:
        assert not xn.__bool__()
        assert n.val is None

https://docs.python.org/3/reference/datamodel.html#object.__bool__

No, but...

You cannot override the is, and, or or operators.

Defining __bool__ allows you to write statements like

class Node:
    def __init__(self, val):
        self.val = val

    def __bool__(self):
        return self.val is not None   # <--- added "return"

for val in (0, 1, True, None):
    n = Node(val)
    # These three are equivalent
    if n:
        assert x.__bool__()
        assert n.val is not None
    # These three are equivalent
    else:
        assert not x.__bool__()
        assert n.val is None

https://docs.python.org/3/reference/datamodel.html#object.__bool__

No, but...

You cannot override the is, and, or or operators.

Defining __bool__ allows you to write statements like

class Node:
    def __init__(self, val):
        self.val = val

    def __bool__(self):
        return self.val is not None   # <--- added "return"

for val in (0, 1, True, None):
    n = Node(val)
    # These three are equivalent
    if n:
        assert n.__bool__()
        assert n.val is not None
    # These three are equivalent
    else:
        assert not n.__bool__()
        assert n.val is None

https://docs.python.org/3/reference/datamodel.html#object.__bool__

added 344 characters in body
Source Link
Cireo
  • 4.5k
  • 2
  • 22
  • 27

No, but...

You cannot override the is, and, or or operators.

Defining __bool__, allows you to write statements like

xclass Node:
    def __init__(self, val):
        self.val = Fooval

    def __bool__(self):
        return self.val is not None   # <--- added "return"

for val in (0, 1, True, None):
    n = Node(val)
    # These three are equivalent
    if xn:
        assert x.__bool__()
        assert n.val is not None
    # These three are equivalent
    else:
        assert not x.__bool__()
        assert n.val is None

https://docs.python.org/3/reference/datamodel.html#object.__bool__

No, but...

You cannot override the is, and, or or operators.

Defining __bool__, allows you to write statements like

x = Foo()
if x:
    assert x.__bool__()
else:
    assert not x.__bool__()

https://docs.python.org/3/reference/datamodel.html#object.__bool__

No, but...

You cannot override the is, and, or or operators.

Defining __bool__ allows you to write statements like

class Node:
    def __init__(self, val):
        self.val = val

    def __bool__(self):
        return self.val is not None   # <--- added "return"

for val in (0, 1, True, None):
    n = Node(val)
    # These three are equivalent
    if n:
        assert x.__bool__()
        assert n.val is not None
    # These three are equivalent
    else:
        assert not x.__bool__()
        assert n.val is None

https://docs.python.org/3/reference/datamodel.html#object.__bool__

added 59 characters in body
Source Link
Cireo
  • 4.5k
  • 2
  • 22
  • 27

No, but...

You could consider definingcannot override the is, and, or or operators.

Defining __bool__, so thatallows you can doto write statements like

x = Foo()
if x:
    assert x.__bool__()
else:
    assert not x.__bool__()

https://docs.python.org/3/reference/datamodel.html#object.__bool__

No, but...

You could consider defining __bool__, so that you can do statements like

x = Foo()
if x:
    assert x.__bool__()
else:
    assert x.__bool__()

https://docs.python.org/3/reference/datamodel.html#object.__bool__

No, but...

You cannot override the is, and, or or operators.

Defining __bool__, allows you to write statements like

x = Foo()
if x:
    assert x.__bool__()
else:
    assert not x.__bool__()

https://docs.python.org/3/reference/datamodel.html#object.__bool__

Source Link
Cireo
  • 4.5k
  • 2
  • 22
  • 27
Loading