first of not None

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Oct 9 14:40:11 CEST 2008

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 = if name is not None else if beny is not None \
> else 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
         return default

More information about the Python-list mailing list