Equal to Anything

Discussion in 'Python' started by Simon Brunning, Oct 4, 2006.

  1. For the purposes of a little test utility that we use, I need an
    object that evaluates as equal to anything. (I realise that this is a
    bad idea in some ways, breaking the equals/hashcode invariant and so
    forth, but I'm hoping that I can get away with it in this case.) It
    seems a simple enough task at first:

    >>> class EqualAnything(object):

    .... def __eq__(self, other):
    .... return True
    ....
    >>> spam = EqualAnything()
    >>> spam == 1

    True
    >>> spam == "hello!"

    True
    >>> spam == datetime.datetime.now()

    True
    >>> 1 == spam

    True
    >>> "hello!" == spam

    True

    But...

    >>> datetime.datetime.now() == spam

    False

    I'm fairly sure that I know what is going on here - the left hand side
    object is getting first stab at the equality test, and understandably,
    it's saying "Nah". But is there anything that I can do about it?

    --
    Cheers,
    Simon B,

    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Oct 4, 2006
    #1
    1. Advertising

  2. Simon Brunning

    Duncan Booth Guest

    "Simon Brunning" <> wrote:

    > I'm fairly sure that I know what is going on here - the left hand side
    > object is getting first stab at the equality test, and understandably,
    > it's saying "Nah". But is there anything that I can do about it?
    >

    Not in general, no. If you could, and someone else created a
    NotEqualAnything class with the identical definition to EqualAnything
    except it returns False, then which would win?

    Sorry, but the left hand value wins unless it volunteers otherwise.
     
    Duncan Booth, Oct 4, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Vecchi

    Need to make control equal to control

    Ron Vecchi, Dec 11, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    448
    Jacob Yang [MSFT]
    Dec 12, 2003
  2. Ralph Krausse

    ALL 'try/catch/finally' NOT created equal?

    Ralph Krausse, Aug 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    426
  3. Ron de Waard
    Replies:
    2
    Views:
    12,619
    HansDampf
    Oct 5, 2007
  4. Emilio
    Replies:
    0
    Views:
    811
    Emilio
    Jul 12, 2004
  5. Jeroen

    Struts <logic:equal>

    Jeroen, Jul 17, 2004, in forum: Java
    Replies:
    0
    Views:
    1,035
    Jeroen
    Jul 17, 2004
Loading...

Share This Page