Menu

#2 improved equality checking

open
nobody
None
5
2009-01-14
2009-01-14
No

I might be the first person to ever equality-test pyparsing grammars, but I need to for pyparsing_helper to work right, and it looks like ParserElement.__eq__ wasn't written to support that.

[[code]]
In [41]: Literal('a') == "a"
Out[41]: True

In [42]: Literal('a') == Literal('a')
Out[42]: False

A minor change to ParserElement.__eq__ will fix that.

Discussion

  • Catherine Devlin

    patch for more powerful equality comparisons

     
  • Paul McGuire

    Paul McGuire - 2009-01-16

    Looks good overall. When I've implemented equality checks in the past, I usually put in a shortcut up front in case an object is being tested for equality with itself. This would change your patched in test to something like:

    return self is other or self.__dict__ == other.__dict__

    When I've proposed something like this in the past on c.l.py, I've gotten mixed responses ("Python is not Java" for instance) - since you have a *real* application that depends on this, could you try making this mod, and see if there is any benefit? If it turns out that you are often testing grammars against themselves, then this could avoid the needless and possibly expensive self.__dict__ equality comparison.

    Let me know what you think, and I'll check in an updated pyparsing.py into SVN.

    -- Paul

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.