Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51965

Re: Bug? ( () == [] ) != ( ().__eq__([]) )

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <markus.rother@web.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.027
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'default.': 0.09; 'false,': 0.09; 'implements': 0.09; 'operand': 0.09; 'operator,': 0.09; 'things,': 0.09; 'defer': 0.16; 'it;': 0.16; 'message-id:@web.de': 0.16; 'unequal,': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'normally': 0.19; 'result.': 0.19; 'not,': 0.20; 'seems': 0.21; 'aug': 0.22; 'header:User-Agent:1': 0.23; 'integer': 0.24; "shouldn't": 0.24; 'equivalent': 0.26; 'second': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; '3.2': 0.31; 'comparison': 0.31; 'object.': 0.31; 'class': 0.32; "we're": 0.32; 'another': 0.32; 'could': 0.34; "can't": 0.35; 'knows': 0.35; 'equal': 0.35; 'but': 0.35; 'false': 0.36; 'object,': 0.36; 'returning': 0.36; 'done': 0.36; 'thanks': 0.36; 'possible': 0.36; 'behind': 0.37; 'received:192.168.2': 0.37; 'handle': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'first': 0.61; 'back': 0.62; 'skip:n 10': 0.64; 'more': 0.64; 'side': 0.67; 'determine': 0.67; 'received:212.227.17': 0.68; 'received:web.de': 0.68; 'reasoning': 0.91; 'directly.': 0.95; '2013': 0.98
Date Mon, 05 Aug 2013 16:58:03 +0200
From Markus Rother <markus.rother@web.de>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130422 Thunderbird/17.0.5
MIME-Version 1.0
To python-list@python.org
Subject Re: Bug? ( () == [] ) != ( ().__eq__([]) )
References <51FED74E.6080003@markusrother.de> <CAPTjJmp=FzJO5pA2WgcojcK4UneAwx8qWDqrC4GeqjO9dxHgfg@mail.gmail.com>
In-Reply-To <CAPTjJmp=FzJO5pA2WgcojcK4UneAwx8qWDqrC4GeqjO9dxHgfg@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V03:K0:Mpt/qaHS/FRaPC9v1XpOVW9KuyWMYZ6u/RRxsV9D3cj3RN3fsOh Rn7tpVEbZz6y5hgS/J5vfVZLb//S7yj1rA0RDV+VYYx3oC1laffLtuEaOQ+Z0v/H5OZJxTP KAIPsnwz0Xm/Ev/gI1b0eZVWKZ5xC4WxrkRiijSNhvHzqWMn4pNOnXjYUUW00/lb9qJHNhS B/gDTeNN9IfGjakS93UOA==
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.212.1375714689.1251.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375714689 news.xs4all.nl 15936 [2001:888:2000:d::a6]:41480
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51965

Show key headers only | View raw


Thanks for the good explanation.

My intention was to pass a custom method/function as a comparator
to an object.  My misconception was, that __eq__ is equivalent to
the '==' operator, and could be passed as a first class function.
Apparently, that is not possible without wrapping the comparison
into another function/method.

Best regards,
Markus R.

On 05.08.2013 01:06, Chris Angelico wrote:
> On Sun, Aug 4, 2013 at 11:35 PM, Markus Rother <python@markusrother.de> wrote:
>> Hello,
>>
>> The following behaviour seen in 3.2 seems very strange to me:
>>
>> As expected:
>>>>> () == []
>> False
>>
>> However:
>>>>> ().__eq__([])
>> NotImplemented
>>>>> [].__eq__(())
>> NotImplemented
>
> You don't normally want to be calling dunder methods directly. The
> reasoning behind this behaviour goes back to a few things, including a
> way to handle "1 == Foo()" where Foo is a custom type that implements
> __eq__; obviously the integer 1 won't know whether it's equal to a Foo
> instance or not, so it has to defer to the second operand to get a
> result. This deferral is done by returning NotImplemented, which is an
> object, and so is true by default. I don't see any particular reason
> for it to be false, as you shouldn't normally be using it; it's more
> like a "null" state, it means "I don't know if we're equal or not". If
> neither side knows whether they're equal, then they're presumed to be
> unequal, but you can't determine that from a single call to __eq__.
>
> ChrisA
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Bug? ( () == [] ) != ( ().__eq__([]) ) Markus Rother <markus.rother@web.de> - 2013-08-05 16:58 +0200

csiph-web