Is there a way to define a class instance is None?

Viewed 1412

For example, when I have such a Node class defined.

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

    def __bool__(self):
        return self.val is not None

When I initialize it with empty arguments, like below. Is there a way to self-define method to say a is None?

a = Node()
a is None # False, but can it be true if I want?
3 Answers

While you cannot override the is comparison, you can at least override the equality operator if you want to quickly check up whether a specific parameter (or condition) within your class should yield True on comparison, e.g.:

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

    def __eq__(self, obj):
        return obj == self.val

n = Node()
print(n == None)  # True
n = Node(5)
print(n == None)  # False

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

This may not do exactly what you want but you could overwrite the __new__ class method so that, when the class constructor is called with no arguments, the None object is returned instead of an instance of Node.

I think this should work (my metaclass knowledge is spotty).

class Node:
    def __new__(cls, val=None, next=None):
        if val is None and next is None:
            return None

        return super().__init__(cls, val, next)

    def __init__(self, val, next):
        if self is None:
            return

        ...

It is my duty to recommend that you not go down this route, however. Fiddling with __new__ is tricky and dangerous and is probably more trouble than it's worth.

Related