PEP 285: Adding a bool type
Alex Martelli
aleax at aleax.it
Tue Apr 9 05:00:55 EDT 2002
Lulu of the Lotus-Eaters wrote:
> |the Lotus-Eaters wrote:
> |> fs = (can_be_moved, can_be_sized, can_be_stacked,
> |> can_be_coloured, can_be_hidden)
> |> major = len([1 for f in fs if f(o)]))
>
> |from operators impor add
> |fs = ...
> |major = reduce(add, [f(o) for f in fs])
>
> Well... my version is two bytes shorter :-) (more if you count the extra
> import).
I personally prefer:
major = len(filter(None, [f(o) for f in fs]))
but I understand your reluctance to mix older features, such as filter,
with newer ones, such as list comprehension, when the latter can do
the job by themselves. With old features only:
major = len(filter(None, map(apply, fs, len(fs)*[o])))
is arguably a bit clunky. However, both of these filter-based
alternatives share the advantages of your solution vs Magnus'.
Alex
More information about the Python-list
mailing list