Re: first of not None

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Chase

    #1

    Re: first of not None

    I need to put in the var property of the first object from the list
    that is not None. Somth like:
    >
    foo = first_of([any, beny, riki,]).name
    >
    Dont want to ugly if-cascade:
    >
    foo = any.name if name is not None else beny.name if beny is not None \
    else riki.name if riki is not None

    assuming you meant "foo = any.name if ***any*** is not None else
    beny.name..."

    If you have a fixed/hard-coded list of elements, you could
    something like:

    foo = (any or beny or riki).name

    If you have dynamic list of elements:

    class NoElementFound( Exception): pass
    def first(iterable) :
    for element in iterable:
    if element: return element
    raise NoElementFound

    lst = [any, beny]
    if condition: lst.append(riki )
    print first(lst).name

    This first() is about the functionality of the SQL Coalesce()
    function.

    Hope this helps,

    -tim



Working...