Default if none

Discussion in 'Python' started by Astley Le Jasper, Apr 29, 2010.

  1. I realise I could roll my own here, but I wondered if there was an
    inbuilt version of this?

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

    def default_if_none(*args):
    for arg in args:
    if arg:
    return arg
    return None

    x = None
    y = 5
    z = 6

    print default_if_none(x,y,z)

    >> 5
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     
    Astley Le Jasper, Apr 29, 2010
    #1
    1. Advertising

  2. Astley Le Jasper

    Chris Rebert Guest

    On Thu, Apr 29, 2010 at 3:16 AM, Astley Le Jasper
    <> wrote:
    > I realise I could roll my own here, but I wondered if there was an
    > inbuilt version of this?
    >
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

    > def default_if_none(*args):
    >    for arg in args:
    >        if arg:
    >            return arg
    >    return None
    >
    > x = None
    > y = 5
    > z = 6
    >
    > print default_if_none(x,y,z)


    If none of the potential values are considered boolean false:

    print x or y or z

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Apr 29, 2010
    #2
    1. Advertising

  3. .... oh ... that simple. Now I feel dumb.

    Thanks!

    ALJ
     
    Astley Le Jasper, Apr 29, 2010
    #3
  4. On Thu, 29 Apr 2010 04:12:29 -0700, Astley Le Jasper wrote:

    > ... oh ... that simple. Now I feel dumb.


    It's really difficult to tell what you're talking about, but I assume
    that you're talking about Chris' solution:

    x or y or z

    Be careful, as Chris' solution is rather risky (read his disclaimer
    again). And the code you give does NOT solve the question you ask. Have
    you tested it with something like this?

    >>> default_if_none(0) == 0

    False
    >>> default_if_none(0) is None

    True

    Your code destroys perfectly legitimate values and replaces them with
    None. That's almost certainly not what you actually want. Chris' solution
    isn't much better, but he warned you about it. Read his post carefully.

    >>> x, y, z = 0, None, None
    >>> x or y or z == 0

    False
    >>> x or y or z is None

    True


    What are you trying to accomplish? Return the first arg that is not None,
    otherwise return None? You need to code your test more carefully, by
    testing for None and nothing but None:

    def default_if_none(*args):
    for arg in args:
    if arg is not None:
    return arg

    You don't need the "return None" at the end, because all Python functions
    do that automatically.

    Here is a one-liner to do it:

    (filter(lambda x: x is not None, args) or [None])[0]

    but frankly the one-liner is ugly and I wouldn't use it. Just use the
    function.



    --
    Steven
     
    Steven D'Aprano, Apr 30, 2010
    #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. David Freeman
    Replies:
    8
    Views:
    7,675
    tcena9
    Feb 16, 2011
  2. length power
    Replies:
    2
    Views:
    104
    Rustom Mody
    Apr 10, 2014
  3. Skip Montanaro
    Replies:
    0
    Views:
    71
    Skip Montanaro
    Apr 10, 2014
  4. Johannes Schneider

    Re: why i have the output of [None, None, None]

    Johannes Schneider, Apr 10, 2014, in forum: Python
    Replies:
    0
    Views:
    61
    Johannes Schneider
    Apr 10, 2014
  5. Terry Reedy
    Replies:
    0
    Views:
    67
    Terry Reedy
    Apr 10, 2014
Loading...

Share This Page