1

I'm using Pyramid web framework to build a web app. There are many times that I find myself doing this:

result = request.params.get('abc', None)
if result:
  result = simplejson.loads(result)
else:
  result = {}

The thing is, sometimes, 'abc' request parameter is not present and the value of "result" would be None. Hence I always have to check if it's None before I perform a simplejson.loads operation or else I would get a TypeError: expected string or buffer exception.

Is there a better/more "pythonic" way of doing this?

1 Answer 1

3

Try this:

result = simplejson.loads(request.params.get('abc', '{}'))
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! Thanks. Sorry I can only accept the answer in another 7 minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.