how to conditionally add a dict in-line

W

Wes James

I have this line:

navs.append(A(' '+str(i+1)+'
',_href=self.action(args=request.args,vars={'_page':i,'_query':request.vars._query
or ''})))

How do I do something like this:

vars={'_page':i, if request.vars._query not None then insert this
key/value pair ('_query':request.vars._query) else insert nothing }

thx,

-wj
 
S

Steven D'Aprano

I have this line:

navs.append(A(' '+str(i+1)+'
',_href=self.action(args=request.args,vars= {'_page':i,'_query':request.vars._query
or ''})))

What a mess. How can you read it?

How do I do something like this:

vars={'_page':i, if request.vars._query not None then insert this
key/value pair ('_query':request.vars._query) else insert nothing }

vars = {'_page': i}
if request.vars._query is not None:
vars['_query'] = request.vars._query


See how simple and clear things are when you give up the insistence on
making everything a one-liner?

But if you *insist* on a one-liner (perhaps because your keyboard has a
broken Enter key):

vars = {'_page': i} if request.vars._query is None else {'_page': i, '_query': request.vars._query}
 
W

Wes James

Steven. Thx

(see my question below...)

I have this line:

navs.append(A(' '+str(i+1)+'
',_href=self.action(args=request.args,vars= {'_page':i,'_query':request.vars._query
or ''})))

What a mess. How can you read it?

How do I do something like this:

vars={'_page':i, if request.vars._query not None then insert this
key/value pair ('_query':request.vars._query) else insert nothing }

vars = {'_page': i}
if request.vars._query is not None:
   vars['_query'] = request.vars._query

Could this be:

vars = {'_page': i}
if request.vars._query:
vars['_query'] = request.vars._query


See how simple and clear things are when you give up the insistence on
making everything a one-liner?

<snip>

-wj
 
S

Steven D'Aprano

vars = {'_page': i}
if request.vars._query is not None:
   vars['_query'] = request.vars._query

Could this be:

vars = {'_page': i}
if request.vars._query:
vars['_query'] = request.vars._query


Instead of typing "request.vars._query" in all the examples, I'm going to
abbreviate it as "x" instead.

"if x is not None" and "if x" are very different things. Consider the
following examples:

.... print "action performed when x is not None"
.... else:
.... print "do nothing"
....
do nothing.... print "action performed when x is not None"
.... else:
.... print "do nothing"
....
do nothing


So far so good. Both pieces of code do the right thing when x actually is
None. But what happens if x is *not* None?

.... print "action performed when x is not None"
.... else:
.... print "do nothing"
....
action performed when x is not None.... print "action performed when x is not None"
.... else:
.... print "do nothing"
....
do nothing



That's clearly wrong, because we know that x isn't None, it is the empty
string. The test "if x" does not test for the same thing as "if x is not
None". "if x" tests the general truth value of any object, and many
objects have a false truth value: None, empty string, 0, [], {} and many
more.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top