[Zope] testing for "REQUEST is not None"
Passin, Tom
tpassin@mitretek.org
Wed, 6 Nov 2002 19:43:00 -0500
[Willem Broekema]
> If a method in a Zope product has REQUEST as optional parameter:
>=20
> def something(self, REQUEST=3DNone):
> ...
>=20
> why is the REQUEST always compared with None:
>=20
> if REQUEST is not None:
> ..
>=20
> instead of just:
>=20
> if REQUEST:
Actually, None, 0, {}, [], and the empty string all evaluate to false in
a boolean test, so=20
if REQUEST
works fine. For example:
d=3D{}
if not d: print 'evaluated as false' # prints 'evaluated as false'
Note that the test=20
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