RE: [Zope] testing for "REQUEST is not None"
[Willem Broekema]
If a method in a Zope product has REQUEST as optional parameter:
def something(self, REQUEST=None): ...
why is the REQUEST always compared with None:
if REQUEST is not None: ..
instead of just:
if REQUEST:
Actually, None, 0, {}, [], and the empty string all evaluate to false in a boolean test, so if REQUEST works fine. For example: d={} if not d: print 'evaluated as false' # prints 'evaluated as false' Note that the test if REQUEST is not None: will return 1 (true) for empty lists, dictionaries, and strings, because they are different objects from the None object, of which there is only one. So if you want different behavior depending on whether the REQUEST might be an empty dictionary (for eaxmple) or might have been omitted (and so defautled to None), you want the long form. For a Zope REQUEST object this would not matter since the REQUEST would never be empty, but it could matter for other parameters. If you want the same behavior for None as for the other empty objects, you should use the short form. I normally use the short form. Cheers, Tom P
participants (1)
-
Passin, Tom