how to conditionally add a dict in-line

Wes James comptekki at gmail.com
Tue Feb 24 05:56:46 CET 2009


Steven. Thx

(see my question below...)

On Mon, Feb 23, 2009 at 9:47 PM, Steven D'Aprano
<steven at remove.this.cybersource.com.au> wrote:
> On Mon, 23 Feb 2009 21:35:38 -0700, Wes James wrote:
>
>> 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



More information about the Python-list mailing list