Re: [web2py] request.vars convenience

2010-04-15 Thread Mladen Milankovic
On Thursday, April 15, 2010 11:23:15 selecta wrote:
 for request.args I can do
 
 request.args(0)
 
 and will return None if there is no such element, much nicer than
 
 request.args[0] if len(request.args)0 else None
 
 I always wondered why this does not work for request.vars I have the
 following all over my code
 
 q = request.vars['q'] if request.vars.has_key('q') else None
 
 did I miss something?

Hi.

You can do:

q = request.vars.get('q', None)

You can put anything instead of None. It's like a default value.

regards
mmlado


Re: [web2py] request.vars convenience

2010-04-15 Thread Jonathan Lundell
On Apr 15, 2010, at 3:05 AM, Mladen Milankovic wrote:

 q = request.vars.get('q', None)
 
 You can put anything instead of None. It's like a default value.

Or just q = request.vars.get('q'), since None is the default.

This is standard dictionary behavior in Python. It and setdefault are 
particularly useful dict methods, and worth getting to know.