I have an API in Python that can return an object, or Noneif the object is not found. I want to avoid exceptions / crashes at runtime, etc., so I want to force users of my API to run a test is not None.
For instance:
x = getObject(...)
if x is not None:
print x.getName()
y = getObject(...)
print y.getName()
How can i achieve this?
In comparable C ++ code, I can add a flag that will be checked when called getName(); the flag is set only when comparing the object with NULL.
In Python, however, I cannot overload the statement is. Are there any other ways I can implement this function in Python?
source
share