<p>Let me see if I understand this:</p>
<p>False in [False]</p>
<p>Returns True now but would return False with your change.</p>
<p>--- Bruce<br>
(via android)</p>
<p><blockquote type="cite">On Jul 25, 2010 11:15 AM, "Alex Gaynor" <<a href="mailto:alex.gaynor@gmail.com">alex.gaynor@gmail.com</a>> wrote:<br type="attribution">Recently I've been wondering why __contains__ casts all of it's<br>
returns to be boolean values. Specifically I'd like to propose that<br>
__contains__'s return values be passed directly back as the result of<br>
the `in` operation. As a result I'd further propose the introduction<br>
of __not_contains__, which is the `not in` operator. The primary<br>
usecase for this is something like an expression recorder. For<br>
example in SQLAlchemy one can do:<br>
<br>
User.id == 3, but not User.id in SQLList([1, 2, 3]), because it returns a<br>
bool always. __not_contains__ is needed to be the analog of this, as<br>
it cannot be merely be a negation of __contains__ when it's returning<br>
a non-bool result.<br>
<br>
There should be no backwards compatibility issues to making<br>
__contains__ return non-bools, unless there is code like:<br>
<br>
x = y in foo<br>
assert type(x) is bool<br>
<br>
However, for __not_contains__ I'd propose the default implementation be:<br>
<br>
def __not_contains__(self, val):<br>
x = val in self<br>
if type(x) is not bool:<br>
raise TypeError("%s returned a non-boolean value from<br>
__contains__ and didn't provide an implementation of<br>
__not_contains__")<br>
return not x<br>
<br>
This is not perfect (and it's at odds with the fact that __ne__<br>
doesn't return not self == other), but it seems to allow both the<br>
desired flexibility and backwards compatibility.<br>
<br>
I'm not sure if this is something that'd be covered by the language<br>
moratorium, but if not I can try putting together a patch for this.<br>
<br>
Alex<br>
<br>
<br>
<br>
--<br>
"I disapprove of what you say, but I will defend to the death your<br>
right to say it." -- Voltaire<br>
"The people's good is the highest law." -- Cicero<br>
"Code can always be simpler than you think, but never as simple as you<br>
want" -- Me<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</blockquote></p>