Most Powerful Open Source ERP

Guideline Never Use Python If Obj if Obj Is Not A Simple Type

Truth testing in Zope/ERP5 can be surprising.
  • Last Update:2018-08-16
  • Version:001
  • Language:en

Never Use Python If Obj if Obj Is Not A Simple Type

Truth value testing on Zope / ERP5 objects has surprising behavior. For example "if obj" returns false if the object is an empty folder, and "if obj" returns true if it is a folder with sub objects. This is because python uses __len__ if __nonzero__ method doesn't exist on object. So "if obj" MUST NOT be used to check if the object is None.

Good Example:

document = context.getFollowUpRelatedValue(portal_type='File')
if document is None:
  document = ...

Bad Example:

document = context.getFollowUpRelatedValue(portal_type='File')
if not document:
  # Here document may exist, but the conditional is False as document doesn't have sub-objects