You are currently browsing the tag archive for the 'Python' tag.
Classes que implementam o método __len__, quando testadas com if, este método é invocado. Portanto, para uma lista vazia, o teste falha.
x = [];
if x:
print "OK" # Nao imprime nada
Para mudar este comportamento, basta reimplementar o método __nonzero__.
class MyList(list):
def __nonzero__(self):
return True
x = MyList();
if x:
print "OK" # Imprime OK







Comentários Recentes