jtcloud
jtcloud

Reputation: 559

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

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?

Upvotes: 6

Views: 5328

Answers (4)

Hasitha Amarathunga
Hasitha Amarathunga

Reputation: 2003

You should initialize the class variable as a parameter. Try below code lines.

class dummy(object):
     empty_array = []
     def __init__(self, size):
        self.empty_array = [[None]*2 for i in range(size)]

a = dummy(3)
print (a.empty_array)

Upvotes: 0

Cireo
Cireo

Reputation: 4427

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

Upvotes: 4

Daniel Walker
Daniel Walker

Reputation: 6760

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.

Upvotes: 2

zwer
zwer

Reputation: 25809

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

Upvotes: 5

Related Questions