In general, is it reasonable to return None from a __new__
method if the user of the class knows that sometimes the constructor will evaluate to None?
The documentation doesn't imply it's illegal, and I don't see any immediate problems (since __init__
is not going to be called, None not being an instance of the custom class in question!). But I'm worried about
- whether it might have other unforeseen issues
- whether it's a good programming practice to have constructors return None
Specific example:
class MyNumber(int):
def __new__(cls, value): # value is a string (usually) parsed from a file
if value == 'N.A.':
return None
return int.__new__(cls, value)