testing for "REQUEST is not None"
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: .. ? - Willem
Andy McKay wrote:
if REQUEST:
Because this tests it against more than being just None. For example an empty dict, string of zero length etc would all pass.
Thanks, but that part I understand. I should have asked: in what case would REQUEST be such a thing, instead of a Request instance? Is it to catch users who deliberately add '&REQUEST=&..' to their query string? - Willem
Hello Willem, Thursday, November 7, 2002, 4:23:36 AM, you wrote: WB> Thanks, but that part I understand. WB> I should have asked: in what case would REQUEST be such a thing, instead WB> of a Request instance? if the method was called directly from somewhere , not through the web.. - so that , if you for instance are adding objects programmatically, will not be redirected to manage_main... :) -- Geir Bækholt geir@funcom.com Tools/HCI-developer Tools/Billing - Product Operations Funcom Oslo
Geir Bækholt wrote:
if the method was called directly from somewhere , not through the web..
- so that , if you for instance are adding objects programmatically, will not be redirected to manage_main...
Also then, the absense of REQUEST can be tested with just 'if REQUEST'?! Obviously I'm still not making clear what I want to know: - 'None' compares to a boolean false - 'def something(self, REQUEST=None):' gives REQUEST the value None, if it is omitted - inside the function, REQUEST is either a Request instance (boolean true) or 'None' (boolean false), so testing for 'if REQUEST is not None:' is equivalent to 'if REQUEST:' As 'if REQUEST' is shorter than '.. is not None', why not use it instead? Like this: def something(self, REQUEST=None): # do actions if REQUEST: # redirect to ... It's equivalent to testing for an empty string: why test for the empty string like this: if s != '' when you know s is a string, so you can just do: if s: - Willem
saying "if REQUEST is not None" is a much more explicit way of saying "this condition fails because REQUEST really was never passed in". since "None" is the default argument for REQUEST you know for a fact that REQUEST was never passed into the function. it is probably true that any other "non-truth" value should make the condition fail, but some people might prefer to have it not fail and throw errors instead to find out that somehow garbage got passed in as the REQUEST argument. jens On Thursday, Nov 7, 2002, at 06:25 US/Eastern, Willem Broekema wrote:
Geir Bækholt wrote:
if the method was called directly from somewhere , not through the web..
- so that , if you for instance are adding objects programmatically, will not be redirected to manage_main...
Also then, the absense of REQUEST can be tested with just 'if REQUEST'?!
Obviously I'm still not making clear what I want to know:
- 'None' compares to a boolean false
- 'def something(self, REQUEST=None):' gives REQUEST the value None, if it is omitted
- inside the function, REQUEST is either a Request instance (boolean true) or 'None' (boolean false), so testing for 'if REQUEST is not None:' is equivalent to 'if REQUEST:'
As 'if REQUEST' is shorter than '.. is not None', why not use it instead?
Like this:
def something(self, REQUEST=None): # do actions if REQUEST: # redirect to ...
It's equivalent to testing for an empty string: why test for the empty string like this: if s != ''
when you know s is a string, so you can just do: if s:
- Willem
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
- inside the function, REQUEST is either a Request instance (boolean true) or 'None' (boolean false), so testing for 'if REQUEST is not None:' is equivalent to 'if REQUEST:'
Nope. A Request instance is not the same thing as boolean True. Instances of user-defined classes are considered False if the class defines a __nonzero__() or __len__() method, and that method returns the integer zero or bool value False. So using a instance as boolean true depends on the implementation of the class... thats why you always need to test for None. You can find more info about this here; http://www.python.org/doc/current/lib/truth.html -- /Magnus
Magnus Heino wrote:
- inside the function, REQUEST is either a Request instance (boolean true) or 'None' (boolean false), so testing for 'if REQUEST is not None:' is equivalent to 'if REQUEST:'
Nope.
A Request instance is not the same thing as boolean True.
I meant it as: in a boolean context the value of the REQUEST object, which is a Request class instance, is 'true', in other words an 'if REQUEST' will always succeed.
So using a instance as boolean true depends on the implementation of the class... thats why you always need to test for None.
REQUEST is an instance of the class Request, which is an alias for HTTPRequest, which is derived from BaseRequest, which has a method __len__ that returns the constant 1, which is 'true', so REQUESTS are *always* 'true'. - Willem
Willem Broekema wrote:
REQUEST is an instance of the class Request, which is an alias for HTTPRequest, which is derived from BaseRequest, which has a method __len__ that returns the constant 1, which is 'true', so REQUESTS are *always* 'true'.
;-) Well I guess you just proved Jens true, who wrote in this thread (read with emphasis on _explicit_):
Saying "if REQUEST is not None" is a much more explicit way of saying "this condition fails because REQUEST really was never passed in". since "None" is the default argument for REQUEST you know for a fact that REQUEST was never passed into the function.
cheers, oliver
Willem Broekema <willem@pastelhorn.com> wrote:
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:
1. it's more explicit (REQUEST is not a boolean) 2. it's faster Florent -- Florent Guillaume, Nuxeo (Paris, France) +33 1 40 33 79 87 http://nuxeo.com mailto:fg@nuxeo.com
participants (7)
-
Andy McKay -
Florent Guillaume -
Geir Bækholt -
Jens Vagelpohl -
Magnus Heino -
Oliver Bleutgen -
Willem Broekema