Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3, module 'itertools' has no attribute 'ifilter'

I am new at Python, trying to build an old python file into Python 3. I got several build errors which I solved. But at this point I am getting above error. I have no idea how to fix this. The code section looks like below.

return itertools.ifilter(lambda i: i.state == "IS", self.storage) 
like image 674
Sohag Mony Avatar asked Nov 15 '15 01:11

Sohag Mony


1 Answers

itertools.ifilter() was removed in Python 3 because the built-in filter() function provides the same functionality now.

If you need to write code that can run in both Python 2 and Python 3, use imports from the future_builtins module (only in Python 2, so use a try...except ImportError: guard):

try:     # Python 2     from future_builtins import filter except ImportError:     # Python 3     pass  return filter(lambda i: i.state == "IS", self.storage) 
like image 149
Martijn Pieters Avatar answered Sep 24 '22 00:09

Martijn Pieters