I have defined a class in Python 3 and have a case that I create an "empty" class object. Therefor I want to be able to check if the object is empty or not, like you can write:
test = []
if not test:
print('False')
My code for the class looks like this (note that I will accept an empty value for name):
class myClass:
def __init__(self, name=False):
self.name = name
def __str__(self):
return self.name.replace('\n', ' ')
def __repr__(self):
return self.name.replace('\n', ' ')
def __eq__(self, other):
if not isinstance(other, myClass):
return False
if not self.name and not other.name:
pass
elif not (self.name or other.name):
return False
elif self.name != other.name:
return False
return True
Now I would like to check if I got an empty class:
test = myClass()
if not test:
print('False')
else:
print('True')
The result of this case will always be true. How can I change this behaviour?