Ivo Shipkaliev <ivo.shipkal...@gmail.com> added the comment:

There are 2 good reasons for making Tk variables comparable by value:

   1. We honor the equality operator! The equality operator (==) has to compare 
two objects by value. The current implementation does not fulfil this. Yes, if 
two Tk variables have the same names in the Tcl interpreter, they are 
guaranteed to have the same value.

>>> a = tk.IntVar(name='a')
>>> b = tk.IntVar(name='a')
>>> assert a == b  # this is not enough; equality means "by value"
                   # what about when they don't have the same name?

>>> assert a is not b  # this again does not make sense, since
                       # both Python "a" and "b" identifiers lead to
                       # the same "self._tk.globalgetvar(self._name)"
                       # Tcl registered variable: name="a"

>>> a.set(42); assert b.get() == a.get() == 42  # yes!
    # equality in names guarantees equality in value

Yes ... BUT, the negation is not true: if two Tk variables have different 
names, that does NOT mean that they have different values. This is where we 
fail the equality operator:

>>> c = tk.IntVar(name='c', value=42)
>>> assert a._name != c._name and a.get() != c.get(), \
...     "Difference in names does not guarantee difference in value"

   2. When we're interested in Tk variable comparison: .get() becomes 
redundant. Every time two Tk variables are of the same type AND they refer to 
the same value, we can safely compare them as equal, regardless of how they are 
named in the Tcl interpreter.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42750>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to