Connecting Tech Pros Worldwide Forums | Help | Site Map

first of not None

Serge Matveenko
Guest
 
Posts: n/a
#1: Oct 9 '08
Hello, everybody!

Could someone help me with coding this thing?

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


--
Serge Matveenko
mailto:serge@matveenko.ru
http://serge.matveenko.ru/

Bruno Desthuilliers
Guest
 
Posts: n/a
#2: Oct 9 '08

re: first of not None


Serge Matveenko a écrit :
Quote:
Hello, everybody!
>
Could someone help me with coding this thing?
>
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
>
def not_none(obj):
return obj is not None

def first_of(seq, predicate=not_none, default=None):
for obj in seq:
if predicate(obj):
return obj
else:
return default

Closed Thread