4

This might be a basic question but I curious to know if I am using filter builtin in my code but pylint reports

W: 67,13: Used builtin function 'filter' (bad-builtin)

how come thats bad-builtin ?

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
  • 1
    Long story short, going forward you should prefer List comprehension instead of `filter`. pylint enforces this with this error message. – thefourtheye Feb 16 '14 at 03:08
  • 3
    feel free to ignore it, especially if it tells you `map` is bad. `map` is awesome. – roippi Feb 16 '14 at 03:10
  • @roippi But we should not use `map` also. List comprehension is the way to go :) – thefourtheye Feb 16 '14 at 03:12
  • 2
    @thefourtheye we'll just have to agree to disagree, this comment box is insufficient space for me to extol the virtues of `map` :) – roippi Feb 16 '14 at 03:14
  • performance wise is it list comprehension better or the builtin `map` ? – Ciasto piekarz Feb 16 '14 at 03:21
  • Depends. See this answer for some old test data: http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map - but either way the choice is almost certainly not what's going to determine your overall performance. – Peter DeGlopper Feb 16 '14 at 03:30

1 Answers1

3

You'll need to decide whether you care about this warning or not.

filter(my_filter, my_iterable)

is equivalent to:

[item for item in my_iterable if my_filter(item)]

In python 3, filter acts like a generator, which means that it lazily evaluates things, e.g.:

(item for item in my_iterable if my_filter(item))

I would ignore this particular warning. Pyflakes may give you a more useful output.

dd.
  • 922
  • 1
  • 7
  • 17