tags:

views:

132

answers:

1

If not, what is the best way to do this?

Right now I'm doing (for a django project):

if not 'thing_for_purpose' in request.session:
    request.session['thing_for_purpose'] = 5

but its pretty awkward. In Ruby it would be:

request.session['thing_for_purpose'] ||= 5

which is much nicer.

+4  A: 

dict has setdefault().

So if request.session is a dict:

request.session.setdefault('thing_for_purpose', 5)
Jon-Eric