jonhadfield / python-hosts

a hosts file manager library written in python
MIT License
125 stars 33 forks source link

Conditionals for remove_all_matching are always True #5

Closed dmtucker closed 8 years ago

dmtucker commented 8 years ago

https://github.com/jonhadfield/python-hosts/blob/devel/python_hosts/hosts.py#L253

self.entries = [x for x in self.entries if not (lambda y: y.address == address)]

Since this lambda is not invoked, this is essentially doing bool(func) which is always True.

Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> bool(bool)
True
Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> bool(bool)
True

This would probably be better:

self.entries = list(filter(lambda entry: entry.address != address, self.entries))
dmtucker commented 8 years ago

Actually, this surprisingly seems to work, but the syntax is misleading about what is happening:

lambda y: y.address and name in y.names(x)

It looks like y.names is a function being invoked to produce a list that name may be in, but the lambda is, in fact, being invoked with x (in which case y and the lambda are not necessary).

edit: This does not work. The implicit bool is what "hides" the error:

>>> [i for i in range(5) if not (lambda x: x + y(i))]
[]
>>> [i for i in range(5) if not (lambda x: x + y(i)) < 3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
TypeError: unorderable types: function() < int()