Can I "hook" variable reassignment?
<p>Python is a language in which the following is possible:</p>

<pre><code>&gt;&gt;&gt; class A(object):
...  def __eq__(self, _):
...   return True
... 
&gt;&gt;&gt; a = A()
&gt;&gt;&gt; a == "slfjghsjdfhgklsfghksjd"
True
&gt;&gt;&gt; a == -87346957234576293847658734659834628572384657346573465
True
&gt;&gt;&gt; a == None
True
&gt;&gt;&gt; a is None
False
</code></pre>

<p>Other things, like <code>gt</code> and <code>lt</code> are also "overloadable"<sup>1</sup> in this way, and this is a great feature, in my opinion.</p>

<p>I'm curious if the <code>=</code> assignment operator is also "overloadable"<sup>1</sup> in a similar kind of fashion, or if I'd have to recompile Python into <a href="http://jython.org" rel="nofollow noreferrer">NotPython</a> to do this.</p>

<p>(As far as I know, classes and objects don't implement some <code>__assign__</code> method; that's implemented with the C bytecode runner/compiler.)</p>

<p>Specifically, I want to implement an LL(<em>k</em>) parser by iterating on a token list, <a href="https://stackoverflow.com/questions/34734137/readable-controllable-iterators">without using an iterator</a> such that I can change the iterator's index arbitrarily to jump to a given token.</p>

<p>The catch is that on a given cycle of the loop, if I've already arbitrarily modified the index in preparation for the next cycle (something I'll surely do a lot) the <em>last</em> thing I want to do is mess up that variable by adding one to it as if it hadn't been changed.</p>

<p>Probably the easiest, simplest solution to this is to have a flag that gets set on jumps and which is reset to <code>False</code> every cycle, but this can and will introduce tiny hiding bugs I'd like to avoid ahead of time. (<a href="https://github.com/catb0t/mouse16/blob/master/mouse16.py#L682" rel="nofollow noreferrer">See here for what I mean -- this is the pathetic iterative LL(<em>1</em>) parser I'm rewriting, and I want its reincarnation to be <em>somewhat</em> maintainable and/or readable</a>.)</p>

<p>The flag solution:</p>

<pre><code>idx = 0
while True:
  jmpd = False
  # maybe we jumped, maybe we didn't; how am I to know!?!?
  if jmpd == False:
    idx += 1 
</code></pre>

<p>Great! One big drawback: at each possible branch resulting in an arbitrary change in the index, I have to set that flag. Trivial, perhaps (obviously in this example), but to me it seems like a harbinger of unreadable, bug-friendly code.</p>

<p><strong>What's the best way, without using a second variable, to test if a value's changed over time?</strong></p>

<p><sup><sup>If your answer is, "there isn't one, just be quiet and use a flag variable", then I congratulate you for promoting bad code design. /s</sup></sup></p>

<hr>

<p><sup>1</sup>Yeah, I know it's not <em>technically</em> operator overloading; have you a better, more quickly understandable term?</p>