how to conditionally add a dict in-line

Discussion in 'Python' started by Wes James, Feb 24, 2009.

  1. Wes James

    Wes James Guest

    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
     
    Wes James, Feb 24, 2009
    #1
    1. Advertising

  2. 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


    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}




    --
    Steven
     
    Steven D'Aprano, Feb 24, 2009
    #2
    1. Advertising

  3. Wes James

    Wes James Guest

    Steven. Thx

    (see my question below...)

    On Mon, Feb 23, 2009 at 9:47 PM, Steven D'Aprano
    <> 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
     
    Wes James, Feb 24, 2009
    #3
  4. On Mon, 23 Feb 2009 21:56:46 -0700, Wes James wrote:

    >> 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:


    >>> x = None
    >>>
    >>> if x is not None:

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

    .... 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?


    >>> x = "" # The empty string is *not* None.
    >>>
    >>> if x is not None:

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

    .... 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.




    --
    Steven
     
    Steven D'Aprano, Feb 24, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    418
    Skip Montanaro
    Aug 15, 2003
  2. Alexander Kozlovsky

    dict!ident as equivalent of dict["ident"]

    Alexander Kozlovsky, May 21, 2006, in forum: Python
    Replies:
    5
    Views:
    365
    Alexander Kozlovsky
    May 22, 2006
  3. Paul Melis

    dict.has_key(x) versus 'x in dict'

    Paul Melis, Dec 6, 2006, in forum: Python
    Replies:
    48
    Views:
    1,331
    Kent Johnson
    Dec 15, 2006
  4. Author
    Replies:
    4
    Views:
    2,787
    Author
    Feb 18, 2009
  5. haile
    Replies:
    1
    Views:
    463
    haile
    Nov 24, 2004
Loading...

Share This Page