0

I'm a newbie to python, writing a python script to keep track of notation in a math paper. If the paper defines values (numbers, groups, etc.) with names "A", "B", "C", "D", I want to easily be able to keep track of equalities between them, so that if I write value("A") = value("B") and value("B") = value("C") then value("A") == value("C") evaluates to True and value("A") == value("D") evaluates to False.

Answers to this question seemed promising, but when I define a class value with an equivalence checking function, it won't let me write value("A") = value("B"). I know I could define a graph with edges (value("A"), value("B")) for each equality, then find its connected components, but this seems complicated and inelegant. Is there a better solution?

Community
  • 1
  • 1
Dmitry Vaintrob
  • 153
  • 1
  • 1
  • 5
  • 1
    You can't write `value("A") = value("B")`. What do you intend that to do? If you just want to "mark" those values as equal, you'll need to define some method so you can do something like `value("A").isEquivalentTo(value("B"))`. However, you'll still have to traverse the equivalence graph to find equivalences that you didn't explicitly set. – BrenBarn Apr 05 '15 at 21:12
  • 1
    Does this have to be solved using *Python*? Looks like the excellent task for a logic programming engine like *Prolog*... – Willem Van Onsem Apr 05 '15 at 21:15
  • @CommuSoft you're probably right, but the very basic script I'm writing isn't worth learning a new language. I suspect this should be possible using python classes and inheritance (things like `5 == 5.0` returning `True`). – Dmitry Vaintrob Apr 05 '15 at 21:33
  • You can define your own way of comparing your objects by tampering with the `__eq__(self, other)` method within your class. I am unsure I have understood what you are trying to achieve though. Clarify the relationship between classes please. – Pynchia Apr 05 '15 at 22:04
  • 1
    Answered by a friend without a stackoverflow account, without using classes: Make `value` a dict. `value = {"A":"A", "B":"B", "C":"C", "D":"D"}` (or use http://stackoverflow.com/questions/8495503/python-identity-dictionary to make an identity dictionary) and then assign values using `value["A"] = value["B"]` etc. – Dmitry Vaintrob Apr 05 '15 at 23:02

0 Answers0