{"id":5914,"date":"2025-02-10T01:31:33","date_gmt":"2025-02-10T09:31:33","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=5914"},"modified":"2025-12-01T00:03:03","modified_gmt":"2025-12-01T08:03:03","slug":"spyder-traps-for-matlab-users-1-by-default-spyders-f5-run-executes-the-script-from-clean-workspace","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2025\/02\/10\/spyder-traps-for-matlab-users-1-by-default-spyders-f5-run-executes-the-script-from-clean-workspace\/","title":{"rendered":"Spyder traps for MATLAB users (1): By default, Spyder&#8217;s F5\/Run executes the script from clean workspace."},"content":{"rendered":"\n<p>This is another example of open source projects not going through a comprehensive use case study before changing the default behavior, which ended up pulling a prank on some users.<\/p>\n\n\n\n<p>This time it&#8217;s Spyder&#8217;s good-intentions trying to proactively prevent user mistakes (such as not keeping track of the workspace) throwing the people who meticulously understand their workspace off. <\/p>\n\n\n\n<p>I was working on a FT4222 device which should not be opened again if it&#8217;s already opened, aka the ft4222 class object exists. So naturally like in MATLAB, at the top of the script I check if the device object already exist and only create\/open it when it&#8217;s not already there, like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if 'dev' in locals():\n    pass\nelse:\n    print('Branch')\n    dev = ft4222.openByDescription('FT4222 A')<\/pre>\n\n\n\n<p>To my surprise it doesn&#8217;t work. <code>'dev' in locals()<\/code> always return <code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">False<\/code> every time I press F5, despite when I check again after the script runs, the variable is indeed in there and <code>'dev' in locals()<\/code> returns <code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">True<\/code>. WTF?!<\/p>\n\n\n\n<p>Turns out I was not alone! Somebody had the exact same idiom as I did. <a href=\"https:\/\/github.com\/spyder-ide\/spyder\/issues\/11015\">Spyder 4 changed the default behavior<\/a>, and we are supposed to manually check this dialog box entry so the scripts do not run off a clean slate when we press F5!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"479\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-1024x479.png\" alt=\"\" class=\"wp-image-5915\" srcset=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-1024x479.png 1024w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-300x140.png 300w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-768x359.png 768w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-500x234.png 500w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image.png 1027w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Spyder 5<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"892\" height=\"686\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-1.png\" alt=\"\" class=\"wp-image-5920\" srcset=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-1.png 892w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-1-300x231.png 300w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-1-768x591.png 768w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-1-390x300.png 390w\" sizes=\"auto, (max-width: 892px) 100vw, 892px\" \/><figcaption class=\"wp-element-caption\">Spyder 6<\/figcaption><\/figure>\n\n\n\n<p>It&#8217;s an extremely terrible idea to have the IDE muck with the state by default. In MATLAB, if we want the script to start with clean state, we either put <code>clear<\/code> at the top of the script or <code>clearvars -except<\/code> to keep the variable.<\/p>\n\n\n\n<p>It&#8217;s even harder to catch the new default insidious behavior of Spyder given it runs the script from a clean slate from F5\/Run then dump the values to the workspace. It&#8217;s now a merge between pre-existing variables in the <code>local()<\/code> workspace and the results of the script from from a blank state! <\/p>\n\n\n\n<p>The people who decided change to this default behaveior certainly didn&#8217;t think through this and rushed to do the obvious to please the careless programmers. If a programmer made a mistake by re-running the script without clearing the workspace and was impacted by the dirty variables, they can always reset everything and get out of this (and learn they should clean up the dirty state through the experience), however, somebody who know what they are doing will not be able to easily find out what they did wrong until they search for a behavior that looked more like a bug from Spyder\/Python! It&#8217;s just horrible design choice! MATLAB doesn&#8217;t casually to throw users off like this. Damn!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Also I looked into code cells <code>#%%<\/code> (MATLAB has the equivalent <code>%%<\/code>), but there&#8217;s another annoyance in Spyder: block commenting through <code>\"\"\"<\/code> or <code>```<\/code> pairs is interpreted as output string from <code>runcelll()<\/code>! In other words, <code>runcelll()<\/code> outputs docstrings! So every time you execute the cell, the code you commented out will be concatenated into one long raw string with escape characters and pollute your console screen! Damn!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spyder annoyances (3): The shortcut key Ctrl+D to reset console doesn&#8217;t work unless there&#8217;s nothing half typed in the console.<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_5914\" class=\"pvc_stats all  \" data-element-id=\"5914\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>This is another example of open source projects not going through a comprehensive use case study before changing the default behavior, which ended up pulling a prank on some users. This time it&#8217;s Spyder&#8217;s good-intentions trying to proactively prevent user &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2025\/02\/10\/spyder-traps-for-matlab-users-1-by-default-spyders-f5-run-executes-the-script-from-clean-workspace\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_5914\" class=\"pvc_stats all  \" data-element-id=\"5914\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10,34],"tags":[],"class_list":["post-5914","post","type-post","status-publish","format-standard","hentry","category-matlab","category-python"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=5914"}],"version-history":[{"count":9,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5914\/revisions"}],"predecessor-version":[{"id":6669,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5914\/revisions\/6669"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=5914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=5914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=5914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}