Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Is this not documented in the <a href="http://docs.python.org/reference/datamodel.html#special-method-names" rel="nofollow noreferrer">Language Reference</a>? Just from a quick look there, it looks like <code>__cmp__</code> is ignored when <code>__eq__</code>, <code>__lt__</code>, etc are defined. I'm understanding that to include the case where <code>__eq__</code> is defined on a parent class. <code>str.__eq__</code> is already defined so <code>__cmp__</code> on its subclasses will be ignored. <code>object.__eq__</code> etc are not defined so <code>__cmp__</code> on its subclasses will be honored.</p> <p>In response to the clarified question: </p> <blockquote> <p>I know that <code>__eq__</code> is called in preferecne to <code>__cmp__</code>, but I'm not clear why <code>y.__eq__(x)</code> is called in preference to <code>x.__eq__(y)</code>, when the latter is what the docs state will happen.</p> </blockquote> <p>Docs say <code>x.__eq__(y)</code> will be called first, but it has the option to return <code>NotImplemented</code> in which case <code>y.__eq__(x)</code> is called. I'm not sure why you're confident something different is going on here.</p> <p>Which case are you specifically puzzled about? I'm understanding you just to be puzzled about the <code>"b" == tsc</code> and <code>tsc == "b"</code> cases, correct? In either case, <code>str.__eq__(onething, otherthing)</code> is being called. Since you don't override the <code>__eq__</code> method in TestStrCmp, eventually you're just relying on the base string method and it's saying the objects aren't equal.</p> <p>Without knowing the implementation details of <code>str.__eq__</code>, I don't know whether <code>("b").__eq__(tsc)</code> will return <code>NotImplemented</code> and give tsc a chance to handle the equality test. But even if it did, the way you have TestStrCmp defined, you're still going to get a false result.</p> <p>So it's not clear what you're seeing here that's unexpected.</p> <p>Perhaps what's happening is that Python is preferring <code>__eq__</code> to <code>__cmp__</code> if it's defined on <em>either</em> of the objects being compared, whereas you were expecting <code>__cmp__</code> on the leftmost object to have priority over <code>__eq__</code> on the righthand object. Is that it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload