vendredi 29 septembre 2017

Python - how to get this condition to work in one line:

Here is the condition:

if not (hasattr(link, "class") and 'gen' in link.parent.get('class')):

The thing is sometimes link has no class attribute so I get:

TypeError: argument of type 'NoneType' is not iterable

Is there anyway I manage this condition to work without adding extra ifs?

Edited: Thanks to John Gordon now I know that I can use get('class', []) and my problem is fixed. But I'm still curious if it was any other way. I mean, what if this solution was not possible, is it possible to get this to work by somehow changing the order of statements or somehow teach if to not go for further if hasattr(link, "class") is false?

Aucun commentaire:

Enregistrer un commentaire