[Zope] testing for "REQUEST is not None"

Passin, Tom tpassin@mitretek.org
Thu, 7 Nov 2002 12:24:59 -0500


[ Willem Broekema]

> Also then, the absense of REQUEST can be tested with just 'if=20
> REQUEST'?!
>=20
Yes, but that statement may be misleading.  You have a parameter called =
REQUEST that may or may not be passing to the function.  If that =
parameter were to be omitted, the function would still construct a local =
variable called "REQUEST", and set it to refer to the None object.  You =
will be testing this local variable, which is certain to exist because =
of the Python defaulting mechanism (REQUEST=3DNone).

On the other hand, if you tried to use the test on a variable that had =
never been constructed, then Python would throw an exception since it =
could not be able to find that name.

>=20
> Obviously I'm still not making clear what I want to know:
>=20
>   - 'None' compares to a boolean false
>=20
>   - 'def something(self, REQUEST=3DNone):' gives REQUEST the value =
None,
>     if it is omitted
>=20
>   - inside the function, REQUEST is either a Request instance (boolean
>     true) or 'None' (boolean false), so testing for

REQUEST is whatever type was passed in, not a boolean true or false.  It =
behaves like a boolean only for the purposes of the logical test.

>     'if REQUEST is not None:' is equivalent to 'if REQUEST:'
>=20

Not quite, as I explained.  The first form tests whether REQUEST is the =
same as the None object.  The second form tests whether REQUEST =
evaluates to true or false when teste - false will be evaluated for 0, =
"", {}, and {} as well as for None. =20

BTW, Javascript actis much the same way.  For example, "undefined" =
evaluates to false in a logical test.

Cheers,

Tom P

> As 'if REQUEST' is shorter than '.. is not None', why not use=20
> it instead?
>=20
> Like this:
>=20
>    def something(self, REQUEST=3DNone):
>      # do actions
>      if REQUEST:
>        # redirect to ...
>=20
>=20
> It's equivalent to testing for an empty string: why test for=20
> the empty=20
> string like this:
>    if s !=3D ''
>=20
> when you know s is a string, so you can just do:
>    if s:
>=20
>=20
> - Willem
>=20
>=20
> _______________________________________________
> Zope maillist  -  Zope@zope.org=20
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross=20
> posts or HTML encoding!  **
> (Related lists -=20
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )
>=20