Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: first of not None

Tim Chase
Guest
 
Posts: n/a
#1: Oct 9 '08
I need to put in the var property of the first object from the list
Quote:
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




Closed Thread