{"id":41991,"date":"2021-07-30T17:48:58","date_gmt":"2021-07-30T17:48:58","guid":{"rendered":"https:\/\/www.oracletutoring.ca\/blog\/?p=41991"},"modified":"2021-07-30T17:48:59","modified_gmt":"2021-07-30T17:48:59","slug":"comp-sci-python-checking-if-a-variable-exists","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/comp-sci-python-checking-if-a-variable-exists\/","title":{"rendered":"Comp Sci: Python: checking if a variable exists"},"content":{"rendered":"\n<h2>Self-tutoring about Python: the tutor mentions a favourite test of his.<\/h2>\n<p>\nIn any programming language, I like to discover how to test the existence of a variable. It&#8217;s more straightforward in some than others.<\/p>\n<p>\nPython really shines in this regard, with its great way of handling exceptions. First of all, it&#8217;s important to realize that when a variable is mentioned that doesn&#8217;t exist, Python gives a NameError exception. In the repl, I entered the following:\n<\/p>\n<p style=\"font-family:monospace\">\ndel var1 # make sure var1 not defined<br\/>\ntry:<br\/>\n<span style=\"margin-left:3em\">&Tab;print(&#8216;var1 is&#8217;,var1)<\/span><br\/>\nexcept NameError:<br\/>\n<span style=\"margin-left:3em\">&Tab;print(&#8216;var1 not defined&#8217;)<\/span>\n<\/p>\n<p>\nPython&#8217;s exception handling mechanism is really convenient: it can steer you around the quicksand of &#8220;not defined&#8221;, etc\n<\/p>\n<p>Source:<\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/843277\/how-do-i-check-if-a-variable-exists\">stackoverflow.com<\/a>: paxdiablo&#8217;s answer<\/p>\nJack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.\n","protected":false},"excerpt":{"rendered":"<p>Self-tutoring about Python: the tutor mentions a favourite test of his. In any programming language, I like to discover how to test the existence of a variable. It&#8217;s more straightforward in some than others. Python really shines in this regard, &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/comp-sci-python-checking-if-a-variable-exists\/\"> <span class=\"screen-reader-text\">Comp Sci: Python: checking if a variable exists<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1856],"tags":[],"class_list":["post-41991","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/41991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=41991"}],"version-history":[{"count":9,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/41991\/revisions"}],"predecessor-version":[{"id":42000,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/41991\/revisions\/42000"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=41991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=41991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=41991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}