Expression can be simplified on list

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Thu Sep 29 04:45:51 EDT 2016


Steven D'Aprano writes:

[- -]

> What is this truthiness abstraction? It is the difference between
> "something" and "nothing".
>
> Values which represent nothing, e.g.:
>
> - None
> - numeric zero: 0, 0.0, 0j, Decimal(0) etc
> - empty strings u'', ''
> - empty containers [], (), {} etc.
>
> are treated as falsey. And values that represent something, e.g.:

[- -]

What do you say about things like iterators and generators? I'd say they
are containers, but they count as true even when they are empty.

bool(x for x in [3,1] if x in [2,7]) # True
list(x for x in [3,1] if x in [2,7]) # []

I think the abstraction really leaks on these things.



More information about the Python-list mailing list