<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[micropython unexpected result]]></title><description><![CDATA[<p dir="auto">OK programming gurus I expected</p>
<pre><code>old_error=0
def _logerrorin(error):
    global old_error
    print('old_error =', old_error)
    print('new_error =', error)
    if error!=old_error: print('a new error')
    old_error=error

for i in range(2):
  try:
    1/0
  except Exception as e: _logerrorin(e)
</code></pre>
<p dir="auto">to print 'a new error' just once, but I actually get</p>
<pre><code>old_error = 0
new_error = divide by zero
a new error
old_error = divide by zero
new_error = divide by zero
a new error
</code></pre>
<p dir="auto">Can someone tell me what I'm doing wrong?</p>
]]></description><link>https://forum.pycom.io/topic/5504/micropython-unexpected-result</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 02:32:24 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/5504.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 17 Dec 2019 02:33:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to micropython unexpected result on Tue, 17 Dec 2019 02:35:00 GMT]]></title><description><![CDATA[<p dir="auto">OK programming gurus I expected</p>
<pre><code>old_error=0
def _logerrorin(error):
    global old_error
    print('old_error =', old_error)
    print('new_error =', error)
    if error!=old_error: print('a new error')
    old_error=error

for i in range(2):
  try:
    1/0
  except Exception as e: _logerrorin(e)
</code></pre>
<p dir="auto">to print 'a new error' just once, but I actually get</p>
<pre><code>old_error = 0
new_error = divide by zero
a new error
old_error = divide by zero
new_error = divide by zero
a new error
</code></pre>
<p dir="auto">Can someone tell me what I'm doing wrong?</p>
]]></description><link>https://forum.pycom.io/post/30912</link><guid isPermaLink="true">https://forum.pycom.io/post/30912</guid><dc:creator><![CDATA[kjm]]></dc:creator><pubDate>Tue, 17 Dec 2019 02:35:00 GMT</pubDate></item><item><title><![CDATA[Reply to micropython unexpected result on Tue, 17 Dec 2019 05:16:37 GMT]]></title><description><![CDATA[<p dir="auto">I am guessing a bit now... Looks like you are compairing two different Exception-objects, they are not the same. You most likley want to compare the description string of the exceptions.</p>
<p dir="auto">Not sure if this is the correct way, but you should get an idea of what to do:</p>
<p dir="auto">if str(error)!=str(old_error): print('a new error')</p>
<p dir="auto">Johan</p>
]]></description><link>https://forum.pycom.io/post/30913</link><guid isPermaLink="true">https://forum.pycom.io/post/30913</guid><dc:creator><![CDATA[johand]]></dc:creator><pubDate>Tue, 17 Dec 2019 05:16:37 GMT</pubDate></item><item><title><![CDATA[Reply to micropython unexpected result on Tue, 17 Dec 2019 16:15:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/2099">@johand</a> Indeed you are correct.</p>
<p dir="auto">As <code>new_error</code> and <code>old_error</code> are <em>Objects</em>, comparing them directly will result in that they are different all the time.</p>
<p dir="auto">Thus, as you suggested one may want to compare the string or something else that Exception throws.</p>
]]></description><link>https://forum.pycom.io/post/30919</link><guid isPermaLink="true">https://forum.pycom.io/post/30919</guid><dc:creator><![CDATA[mnovella]]></dc:creator><pubDate>Tue, 17 Dec 2019 16:15:38 GMT</pubDate></item><item><title><![CDATA[Reply to micropython unexpected result on Tue, 17 Dec 2019 16:43:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/3449">@kjm</a> e is an instance of the class &lt;class 'ZeroDivisionError'&gt;. With every new Exception raised, you create a new instance. Both instances have a different id. In MicroPython as well a Python, class instances are equal if their id is the same, which is not the case here.<br />
If that logic is important for you, you can for instance compare the types in line 6:<br />
<code>    if type(error) != type(old_error): print('a new error')</code></p>
]]></description><link>https://forum.pycom.io/post/30920</link><guid isPermaLink="true">https://forum.pycom.io/post/30920</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Tue, 17 Dec 2019 16:43:55 GMT</pubDate></item></channel></rss>