[Edu-sig] Interesting "gotcha"
kirby urner
kirby.urner at gmail.com
Wed Mar 30 21:18:14 CEST 2011
On Wed, Mar 30, 2011 at 11:31 AM, Edward Cherlin <echerlin at gmail.com> wrote:
> So 'in' is a comparison "operator", is it? I am annoyed at how long it
> took me to verify that Python treats it as such, and I am also annoyed
> that it is so.
>
> http://docs.python.org/tutorial/datastructures.html
> 5.7. More on Conditions¶
>
> The conditions used in while and if statements can contain any
> operators, not just comparisons.
>
As you no doubt know, in triggers the rib __contains__ in the cosmic
backbone of Python's special names list, whereas operators like ==,
> and < trigger their own specially named reflexes:
>>> class Foo:
def __contains__(self, value):
print ("Yes {} is in the bar".format(value))
return True
>>> bar = Foo()
>>> "Joe" in bar
Yes Joe is in the bar
True
>>> class Average:
def __lt__(self, value):
print("Yes {} is better than Average".format(value))
return True
>>> bar = Average()
>>> "Joe's bar" > bar
Yes Joe's bar is better than Average
True
>>>
> The comparison operators in and not in check whether a value occurs
> (does not occur) in a sequence.
>
> I never cared for the misleading a<b<c notation anyway, and won't use it.
>
>
heretic!
Kirby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20110330/4a0eec77/attachment.html>
More information about the Edu-sig
mailing list