Default if none

Chris Rebert clp2 at rebertia.com
Thu Apr 29 06:25:57 EDT 2010


On Thu, Apr 29, 2010 at 3:16 AM, Astley Le Jasper
<astley.lejasper at gmail.com> 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



More information about the Python-list mailing list