first of not None

  • Thread starter Serge Matveenko
  • Start date
S

Serge Matveenko

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
 
B

Bruno Desthuilliers

Serge Matveenko a écrit :
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top