{"id":301624,"date":"2021-07-17T16:00:27","date_gmt":"2021-07-17T13:00:27","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/python-errors-you-probably-made-one-of-these-silly-mistakes\/"},"modified":"2021-07-17T16:00:27","modified_gmt":"2021-07-17T13:00:27","slug":"python-errors-you-probably-made-one-of-these-silly-mistakes","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/python-errors-you-probably-made-one-of-these-silly-mistakes\/","title":{"rendered":"#Python errors? You probably made one of these silly mistakes"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a37cb13d3f65\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a37cb13d3f65\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/python-errors-you-probably-made-one-of-these-silly-mistakes\/#Silly_but_common_mistakes_to_check_for_first\" >Silly but common mistakes to check for first<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/python-errors-you-probably-made-one-of-these-silly-mistakes\/#How_to_avoid_silly_mistakes\" >How to avoid silly mistakes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/python-errors-you-probably-made-one-of-these-silly-mistakes\/#The_bottom_line_avoid_facepalm-moments_for_yourself_and_your_colleagues\" >The bottom line: avoid facepalm-moments for yourself and your colleagues<\/a><\/li><\/ul><\/nav><\/div>\n<p>&#8220;<strong>#Python errors? You probably made one of these silly mistakes<\/strong>&#8221;<\/p>\n<div>\n<p id=\"5a26\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu lk\" data-selectable-paragraph=\"\">Fail fast, fail early \u2014 we\u2019ve all heard<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw mb\" href=\"https:\/\/hbr.org\/2011\/04\/failing-by-design\">the motto<\/a>. Still, it\u2019s frustrating when you\u2019ve written a beautiful piece of code, just to realize that it doesn\u2019t work as you\u2019d expected.<\/p>\n<p id=\"013c\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">That\u2019s where unit tests come in. Checking each piece of your code helps you localize and fix your bugs.<\/p>\n<p id=\"3f18\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">But not all bugs are created the same. Some bugs are\u00a0unexpected, not obvious to see at all, and hard to fix even for experienced developers. These are more likely to occur in large and complex projects, and spotting them early can save you a ton of time later on.<\/p>\n<p id=\"31b7\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Other bugs are trivial, like when you\u2019ve forgotten a closing bracket or messed up some indentations. They\u2019re easy to fix, but hard to spot, especially when you\u2019ve been working on the code for a while or when it\u2019s late at night.<\/p>\n<p id=\"8929\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Once you\u2019ve spotted a bug like this, it\u2019s a bit of a facepalm moment. You could kick yourself for not having seen it earlier \u2014 and you wonder why you did such a stupid mistake in the first place. They\u2019re also not the type of bug that you\u2019d want your colleagues to spot before you do.<\/p>\n<h2 id=\"2652\" class=\"nb nc gx bf nd ne nf ks ng nh ni kv nj nk nl nm nn no np nq nr ns nt nu nv nw hu\"><span class=\"ez-toc-section\" id=\"Silly_but_common_mistakes_to_check_for_first\"><\/span>Silly but common mistakes to check for first<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p id=\"13c9\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">I can\u2019t claim that this list covers all silly mistakes that you\u2019ll ever make. However, using it regularly should at least help you eliminate the most common ones.<\/p>\n<p id=\"1731\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>Forgot that closing brace?<\/strong><\/p>\n<p id=\"2fac\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">It h<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ens to everyone \u2014 you happily code away, and in the flow of it you forget to close that array, argument list, or whatever you\u2019re dealing with.<\/p>\n<p id=\"760a\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Some developers type a closing brace as soon as they open one, and then fill the space in between. Most<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw mb\" href=\"https:\/\/towardsdatascience.com\/what-your-favorite-text-editor-reveals-about-your-personality-f3d4f71f88c2\">modern IDEs<\/a><span>\u00a0<\/span>also close braces automatically \u2014 so if forgetting braces is a chronic disease of yours, you might consider leaving your old IDE behind.<\/p>\n<p id=\"fe61\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>What about your colons?<\/strong><\/p>\n<p id=\"2f66\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">Hey, it happens to the best of us. You\u2019re constructing a new class, and since it\u2019s complex, you\u2019re already thinking about the contents of the class while your typing the code. And whoops, you\u2019ve forgotten that colon at the end:<\/p>\n<p data-selectable-paragraph=\"\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1360933 aligncenter js-lazy\" alt=\"python code\" width=\"679\" height=\"87\" sizes=\"auto, (max-width: 679px) 100vw, 679px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53.png 679w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53-280x36.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53-540x69.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53-270x35.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1360933 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53.png\" alt=\"python code\" width=\"679\" height=\"87\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53.png 679w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53-280x36.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53-540x69.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.19.53-270x35.png 270w\"\/><\/noscript><\/figure>\n<\/p>\n<p id=\"3bce\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">A good rule of thumb is that if you\u2019re increasing the indent of a line, you\u2019ll need to add a colon to the line that comes before it.<\/p>\n<p id=\"f4e2\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>Have you confused equality and assignment operators?<\/strong><\/p>\n<p class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\">When do you use<span>\u00a0<\/span><code>=<\/code>, and when do you use<span>\u00a0<\/span><code>==<\/code>? As a rule of thumb, if you\u2019re checking or comparing two values, you\u2019ll use<span>\u00a0<\/span><code>==<\/code>. On the other hand, you\u2019ll use<span>\u00a0<\/span><code>=<\/code><span>\u00a0<\/span>if you\u2019re assigning a value to a variable.<\/p>\n<p><strong>Have you muddled with mutable expressions?<\/strong><\/p>\n<p id=\"7ef8\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">Have a look at this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1360934 js-lazy\" alt=\"python errors\" width=\"682\" height=\"70\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47.png 682w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47-280x29.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47-540x55.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47-270x28.png 270w\"\/><\/p>\n<p><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1360934\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47.png\" alt=\"python errors\" width=\"682\" height=\"70\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47.png 682w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47-280x29.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47-540x55.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.22.47-270x28.png 270w\"\/><\/noscript><\/p>\n<p id=\"9164\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>Python is case-sensitive, remember?<\/strong><\/p>\n<p id=\"4cf5\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">This is trivial as hell but happens all the time. Say you\u2019ve defined the variable\u00a0<code>CamelBucket<\/code>, and later in the code you call<span>\u00a0<\/span><code>camelbucket<\/code>.<\/p>\n<p id=\"5a76\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Won\u2019t work, huh? Chances are, you know very well that Python is case-sensitive, but you just forgot to press that shift key.<\/p>\n<p id=\"a354\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Most modern IDEs can help you avoid this mistake by making smart suggestions. So if you\u2019re prone to typos, you might consider upgrading your text editor.<\/p>\n<div class=\"mc md me mf mg mh\">\n<p id=\"6a6a\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>Are you modifying lists while iterating over them?<\/strong><\/p>\n<p id=\"7997\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">This has probably happened to every junior developer out there: you\u2019ve built a list, and now you want to change a few things. No big deal, right?<\/p>\n<p id=\"58de\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Wrong. Consider this:<\/p>\n<p data-selectable-paragraph=\"\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1360935 aligncenter js-lazy\" alt=\"python\" width=\"680\" height=\"93\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28.png 680w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28-280x38.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28-540x74.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28-270x37.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1360935 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28.png\" alt=\"python\" width=\"680\" height=\"93\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28.png 680w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28-280x38.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28-540x74.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-16-at-14.24.28-270x37.png 270w\"\/><\/noscript><\/figure>\n<\/p>\n<p id=\"0280\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Spoiler alert: this throws an error because you end up iterating over items in a list that don\u2019t exist anymore. Instead of deleting from an existing list, consider writing to a new one.<\/p>\n<p id=\"56e1\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>Do you have circular module dependencies?<\/strong><\/p>\n<p id=\"94f2\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">This is so trivial, but also\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw mb\" href=\"https:\/\/www.toptal.com\/python\/top-10-mistakes-that-python-programmers-make\">frustratingly common<\/a>: in one file, let\u2019s say<code>bug.py<\/code>, you write<code>import anotherbug<\/code>. In another file,<code>anotherbug.py<\/code>, you write<code>import bug<\/code>.<\/p>\n<p id=\"a13c\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">This isn\u2019t going to work \u2014 how should the computer know which file to include in which? You probably knew this but did it by accident. No worries, but fix it asap!<\/p>\n<p id=\"bdf0\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>You haven\u2019t named your modules like Python\u2019s standard libraries, right?<\/strong><\/p>\n<p id=\"0eb8\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">Another thing regarding modules: Python comes with a wealth of amazing library modules. But if you need to create your own, be sure to give them an original name.<\/p>\n<p id=\"57c4\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">For example, defining a module called<code>numpy.py<\/code>is going to lead to confusion. If you\u2019re in doubt which names you can use, there\u2019s a\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw mb\" href=\"https:\/\/docs.python.org\/3\/library\/\">complete list\u00a0<\/a>of Python\u2019s standard libraries.<\/p>\n<\/div>\n<h2 id=\"dd88\" class=\"nb nc gx bf nd ne nf ks ng nh ni kv nj nk nl nm nn no np nq nr ns nt nu nv nw hu\"><span class=\"ez-toc-section\" id=\"How_to_avoid_silly_mistakes\"><\/span><strong>How to avoid silly mistakes<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p id=\"721a\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">You learn from your mistakes. As a beginner-level developer, you might still be making mistakes like these every day. But as time moves on, they become less and less.<\/p>\n<p id=\"a70e\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">You can also take it to the next level and adopt some healthy habits so you never make trivial mistakes. These are just a few tips of many, but they can help you avoid many bugs.<\/p>\n<p id=\"8a44\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>Always initialize variables<\/strong><\/p>\n<p id=\"3bc3\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">Python code works perfectly if you<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw mb\" href=\"https:\/\/learning-python.com\/reconstruct-learn_python.html\">define a variable<\/a><span>\u00a0<\/span>and don\u2019t assign a value until later. It therefore seems cumbersome to initialize them every time upon definition.<\/p>\n<p id=\"f372\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">But don\u2019t forget that you\u2019re human, and as such you\u2019re prone to losing track of which variable has got assigned and which one hasn\u2019t. Also, initializing variables forces you to think about what type of variable you\u2019re dealing with, which might prevent bugs down the line.<\/p>\n<p id=\"1bc8\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">That\u2019s why many seasoned developers initialize every variable with default values such as<span>\u00a0<\/span><code>0<\/code>,<span>\u00a0<\/span><code>None<\/code>,<span>\u00a0<\/span><code>[]<\/code>,<span>\u00a0<\/span><code>\"\"<\/code>, and so on.<\/p>\n<p id=\"4ae6\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Of course, if you\u2019re just trying to tie up a little standalone script and not working on a mammoth-sized project, you can allow yourself to do things quick and sloppy. But remember to be diligent when things get complex.<\/p>\n<p id=\"9971\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>Use braces to call functions and avoid trouble<\/strong><\/p>\n<p id=\"e3a6\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">Braces again. Somehow they\u2019re just not compatible with the sloppiness of human brains.<\/p>\n<p id=\"447c\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">In Python, you\u2019ll always need to call a function like this:<span>\u00a0<\/span><code>callingthisfunction()<\/code>, and not like this:<span>\u00a0<\/span><code>notcallingthisfunction<\/code>, whether there are any arguments or not. Sounds trivial, but isn\u2019t always!<\/p>\n<p id=\"2f50\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">For example, if you have a file called<span>\u00a0<\/span><code>file<\/code>\u00a0and you want to close it in Python, you\u2019ll write\u00a0<code>file.close()<\/code>. The code will run without throwing an error, though, if you write\u00a0<code>file.close<\/code><span>\u00a0<\/span>\u2014 except that it won\u2019t close the file. Try to locate a mistake like that in a project with thousands of lines of code\u2026<\/p>\n<p id=\"15b1\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">It becomes pretty automatic after a few days of practicing, so start today: functions are always called with braces!<\/p>\n<p id=\"dc21\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>Don\u2019t use extensions while importing modules<\/strong><\/p>\n<p id=\"6e9e\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">Again, this is rather trivial but often forgotten by beginners. If you\u2019re calling a module, you\u2019ll never use an extension, regardless whether it\u2019s from the Python standard library or elsewhere.<\/p>\n<p id=\"280c\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">So watch out for lines like:<\/p>\n<pre class=\"ol om on oo op oq or os\"><span id=\"4216\" class=\"hu oc nc gx ot b df ou ov s ow\" data-selectable-paragraph=\"\">import numpy.py         <em class=\"pk\"># don't do that!!!<\/em><\/span><\/pre>\n<p id=\"c658\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Delete that<span>\u00a0<\/span><code>.py<\/code>\u00a0extension and avoid it like stink. Extensions and imports don\u2019t work together.<\/p>\n<p id=\"7f6e\" class=\"oc nc gx bf nd od oe hy ng of og ib nj ic oh ie nn if oi ih nr ii oj ik nv ok hu\"><strong>Indent consistently<\/strong><\/p>\n<p id=\"290b\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">Again, you probably know yourself that you need to index the same way throughout a file. But it\u2019s still a common pitfall when you\u2019re collaborating with others or when it\u2019s late at night.<\/p>\n<p id=\"0e48\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Before you start coding, decide whether you use tabs or spaces, and how many spaces. The most common convention is four spaces, but you can do whatever suits you best \u2014 as long as you keep it up throughout the document.<\/p>\n<p id=\"2025\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">If, on the other hand, you use spaces in one line and tabs in another, sometimes Python doesn\u2019t treat that like you\u2019d expected. I know it seems like a dumb job, but try to be as diligent as you can about that<\/p>\n<article class=\"meteredContent\">\n<div>\n<section class=\"gq gr gs eb gt\">\n<div class=\"n p\">\n<div class=\"au av aw ax ay gu ba v\">\n<h2 id=\"8afb\" class=\"nb nc gx bf nd ne nf ks ng nh ni kv nj nk nl nm nn no np nq nr ns nt nu nv nw hu\"><span class=\"ez-toc-section\" id=\"The_bottom_line_avoid_facepalm-moments_for_yourself_and_your_colleagues\"><\/span>The bottom line: avoid facepalm-moments for yourself and your colleagues<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p id=\"7e4b\" class=\"ko kp gx kq b hw nx ks kt hz ny kv kw kx nz kz la lb oa ld le lf ob lh li lj gq hu\" data-selectable-paragraph=\"\">Fail fast, fail early \u2014 but don\u2019t fail stupidly. Everybody makes mistakes, but still, it\u2019s better to avoid the stupid ones, or at least fix them quickly.<\/p>\n<p id=\"980b\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">It\u2019s not only about making progress. It\u2019s also about avoiding facepalm-moments where you kick yourself for not having spotted a bug earlier.<\/p>\n<p id=\"ae63\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">And it\u2019s about not losing your reputation \u2014 who wants to run to a colleague or a manager with a seemingly complicated problem, only to find out that it was really easy to fix?<\/p>\n<p id=\"6bbb\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">Chances are that you\u2019ve done some of the mistakes I\u2019ve mentioned in the past. Chill, buddy, I have, too \u2014 we\u2019re human.<\/p>\n<p id=\"551c\" class=\"ko kp gx kq b hw kr ks kt hz ku kv kw kx ky kz la lb lc ld le lf lg lh li lj gq hu\" data-selectable-paragraph=\"\">The point is, if your code behaves in unexpected ways, you\u2019ll want to check for the trivial things first. Then, if your code still doesn\u2019t work properly, you can always ping your colleague.<\/p>\n<p data-selectable-paragraph=\"\"><em>This article was written by<span>\u00a0<\/span><span data-sheets-value=\"{\" moutafis=\"\" data-sheets-userformat=\"{\">Rhea Moutafis\u00a0<\/span>and was originally published on<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>. You can read it<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/towardsdatascience.com\/if-your-python-code-throws-errors-check-these-things-first-a93d8a9036f1\">here<\/a>.\u00a0<\/em><\/p>\n<\/div>\n<\/div>\n<\/section>\n<\/div>\n<\/article>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong>\n<\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/thenextweb.com\/news\/python-errors-check-for-mistakes-syndication\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Python errors? You probably made one of these silly mistakes&#8221; Fail fast, fail early \u2014 we\u2019ve all heard\u00a0the motto. Still, it\u2019s frustrating when you\u2019ve written a beautiful piece of code, just to realize that it doesn\u2019t work as you\u2019d expected. That\u2019s where unit tests come in. Checking each piece of your code helps you localize&#8230;<\/p>\n","protected":false},"author":1,"featured_media":301625,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/img-cdn.tnwcdn.com\/image\/tnw?filter_last=1&fit=1280,640&url=https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/python-mistakes.jpg&signature=de0e72572215b2e42cccc9d4edcebce4","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-301624","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/301624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=301624"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/301624\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/301625"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=301624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=301624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=301624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}