{"id":505,"date":"2022-12-28T13:00:24","date_gmt":"2022-12-28T13:00:24","guid":{"rendered":"https:\/\/pc-keeper.tech\/index.php\/2022\/12\/28\/python-style-guide-ieee-computer-society\/"},"modified":"2022-12-28T13:00:24","modified_gmt":"2022-12-28T13:00:24","slug":"python-style-guide-ieee-computer-society","status":"publish","type":"post","link":"https:\/\/pc-keeper.tech\/index.php\/2022\/12\/28\/python-style-guide-ieee-computer-society\/","title":{"rendered":"Python Style Guide | IEEE Computer Society"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-320837 img-responsive alignright\" src=\"https:\/\/ieeecs-media.computer.org\/wp-media\/2022\/12\/27232231\/The-importance-of-following-a-Python-style-guide.jpg\" alt=\"The importance of following a Python style guide\" width=\"250\" height=\"250\" srcset=\"https:\/\/ieeecs-media.computer.org\/wp-media\/2022\/12\/27232231\/The-importance-of-following-a-Python-style-guide.jpg 250w, https:\/\/ieeecs-media.computer.org\/wp-media\/2022\/12\/27232231\/The-importance-of-following-a-Python-style-guide-150x150.jpg 150w, https:\/\/ieeecs-media.computer.org\/wp-media\/2022\/12\/27232231\/The-importance-of-following-a-Python-style-guide-100x100.jpg 100w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\"\/>Writing code is one thing; writing beautiful, easily understandable code that others can use just as easily as you is a completely different matter altogether. It\u2019s not always easy or intuitive to create neat code in Python, which begs the age-old question, do we have to? And what does neat, clear code even look like?<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">We\u2019re going to break down what makes a Python style guide necessary, as well as the most important elements of a style guide. Read on to learn how you can perfect the way you write your Python code.<\/p>\n<p>\u00a0<\/p>\n<h2 style=\"color: #002855; font-size: 24px; font-family: Montserrat; font-weight: 500; line-height: 29px;\">Why do you need a style guide?<\/h2>\n<hr style=\"text-align: left; width: 30%; height: 3px; color: #ffa300; background-color: #ffa300; border: none;\"\/>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">How often have you come across a piece of code that took you far longer to understand than to read?<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Chances are, if you regularly work with code written by other people, you\u2019ll have had at least a few run-ins with some messy, barely-comprehensible code that makes you wish you had time to rewrite all of it. That\u2019s the direct result of unclearly written code. It leads to poor code reuse and maintainability, which means it can\u2019t be used in other places or easily maintained.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">With a style guide, you can make sure that your own code never puts anyone else\u2014including yourself\u2014in that position.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Style guides see to two absolutely vital things in your code: legibility and consistency. By setting out rules for exactly how things should be written, style guides help you guarantee your coding style will be consistent all across the board. They also ensure that the status quo is legible code.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Let\u2019s consider an analogy: say you\u2019re looking for a consulting proposal template. The first one you come across switches between American and British English spellings, the second one\u2019s layout is confusing, so you don\u2019t know what to enter where, and the third uses a font that\u2019s so hard to read, it slows you down. Without a style guide, that\u2019s what unclear code looks like.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Now let\u2019s consider what happens when the template creators use a style guide. Suddenly, you\u2019ve got scannable proposal templates that are both easy to read and simple to use, because the font and layout are clear, and the spelling style is consistent. That\u2019s the effect a style guide should have on your code.<\/p>\n<p>\u00a0<\/p>\n<hr style=\"width: 100%;\"\/>\n<p>\u00a0<\/p>\n<p style=\"text-align: center; color: #ff6600;\"><strong>Want More Career-focused News? Subscribe to Build Your Career Newsletter Today!<\/strong><\/p>\n<p>\u00a0<\/p>\n<hr style=\"width: 100%;\"\/>\n<p>\u00a0<\/p>\n<p>Free to use image sourced from Pexels<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">That\u2019s why we need style guides like Python Enhancement Proposal 8 (PEP 8). Next, we\u2019ll take a look at some of the most important components of that guide.<\/p>\n<p>\u00a0<\/p>\n<h2 style=\"color: #002855; font-size: 24px; font-family: Montserrat; font-weight: 500; line-height: 29px;\">Indents<\/h2>\n<hr style=\"text-align: left; width: 30%; height: 3px; color: #ffa300; background-color: #ffa300; border: none;\"\/>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">When you look at a segment of code, before you even read it, the first thing that strikes the eye is the way in which the person who wrote it used indents. That\u2019s why it\u2019s one of the most important aspects of a Python style guide.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">The expected standard in Python is to use four spaces. This creates a clean look for your code, and makes it easier to find what you\u2019re looking for at a glance.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Here\u2019s an example of the difference it can make:<\/p>\n<pre style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">#Correct use of indentation\nIf (your_first_condition and\n    your_second_condition):\n    perform_specified_action\n\n#Incorrect use of indentation\nIf (your_first_condition and\nyour_second_condition):\nperform_specified_action\n\n#Another incorrect use of indentation\nIf (your_first_condition and\n\t\tyour_second_condition):\n\tperform_specified_action\n<\/pre>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Both of the latter examples make your code less legible. In the case of the first one, your eyes don\u2019t intuitively know where the function stops, while the second one looks sloppy and somewhat random. In contrast, the first example is clean, well-laid-out, and easy to take in at a glance.<\/p>\n<p>\u00a0<\/p>\n<h2 style=\"color: #002855; font-size: 24px; font-family: Montserrat; font-weight: 500; line-height: 29px;\">Names<\/h2>\n<hr style=\"text-align: left; width: 30%; height: 3px; color: #ffa300; background-color: #ffa300; border: none;\"\/>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Sure, it might be impossible to get every name fully consistent across objects, functions, and more, but that doesn\u2019t mean we shouldn\u2019t try. The more consistent your naming conventions are, the neater your code looks and the easier it is to find what you need.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">One aspect of this is patterns. For example, let\u2019s say you\u2019re naming variables that relate to free electronic signatures. Here\u2019s how to, and how not to, go about that:<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">#Good example<br \/>FreeSigs, FreeElectronicSigs, ElectronicSigsFree<\/p>\n<p>#Bad example<br \/>freeSignatures, FreeElecSigs, electronic_signatures_free<\/p>\n<p>As you can see, following one pattern consistently leads to a cleaner look. In the same way, you\u2019ll want to find a good balance between overly long names, and names that are too short to convey any meaning.<\/p>\n<p>#Good example<br \/>free_signatures, elec_signatures_free<\/p>\n<p>#Bad example<br \/>electronic_signatures_available_for_free_online, esigfree, esf<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">You\u2019ll also want to avoid starting a name of any kind with a number, or including special characters, especially if those characters are likely to crop up elsewhere in your code. Instead, do the right thing and choose clear, concise, no-nonsense names that anyone can understand.<\/p>\n<p>\u00a0<\/p>\n<h2 style=\"color: #002855; font-size: 24px; font-family: Montserrat; font-weight: 500; line-height: 29px;\">Spaces<\/h2>\n<hr style=\"text-align: left; width: 30%; height: 3px; color: #ffa300; background-color: #ffa300; border: none;\"\/>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Blank lines and whitespaces can go a long way towards improving a code.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">For example, if you\u2019re writing multiple functions, it can get confusing pretty quickly if you don\u2019t implement spaces between each individual one. Even if they\u2019re not strictly necessary to make the code work, they help immensely with boosting legibility.<\/p>\n<p>Free to use image sourced from Pexels<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">However, with that said, there are rules surrounding whitespaces. They shouldn\u2019t be used immediately after a bracket, or before commas, semicolons, or colons. They also shouldn\u2019t separate characters and opening brackets, regardless of whether it visually spaces things out.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Assignment operators, comparisons, and booleans should always be surrounded by whitespaces. This helps them remain legible and clear to readers.<\/p>\n<p>\u00a0<\/p>\n<h2 style=\"color: #002855; font-size: 24px; font-family: Montserrat; font-weight: 500; line-height: 29px;\">Comments<\/h2>\n<hr style=\"text-align: left; width: 30%; height: 3px; color: #ffa300; background-color: #ffa300; border: none;\"\/>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Your code shouldn\u2019t be written like a private journal, because it\u2019s not only for you to read. Instead, you should use comments to address future readers and provide necessary clarification.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">When writing comments, ensure you\u2019re as clear and concise as possible. This isn\u2019t the place for flowery language or extra words\u2014get your point across in as few words as you can, and move on.<\/p>\n<p>\u00a0<\/p>\n<h2 style=\"color: #002855; font-size: 24px; font-family: Montserrat; font-weight: 500; line-height: 29px;\">Python style guide: parting thoughts<\/h2>\n<hr style=\"text-align: left; width: 30%; height: 3px; color: #ffa300; background-color: #ffa300; border: none;\"\/>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">It\u2019s clear that a style guide creates rules that lead to cleaner, more legible code. That\u2019s a wonderful thing for people who regularly work with code\u2014and for anyone who\u2019s trying to learn from the way others do things.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">At the same time, a style guide doesn\u2019t have to dictate every aspect of a piece of code. It should establish the way that code should look and be written, but it shouldn\u2019t act as a way to restrict or limit the contents of your code.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">In other words, your Python style guide should make it easier, never harder, to write more code.<\/p>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\">Following the rules the style guide sets out should always help you create code you can be proud of. When the code looks beautiful and is legible to just about anyone who tries to read it, it\u2019s a sign you\u2019ve written great code.<\/p>\n<p>\u00a0<\/p>\n<h2 style=\"color: #002855; font-size: 24px; font-family: Montserrat; font-weight: 500; line-height: 29px;\">About the Writer<\/h2>\n<hr style=\"text-align: left; width: 30%; height: 3px; color: #ffa300; background-color: #ffa300; border: none;\"\/>\n<p style=\"color: #454545; font-size: 18px; font-family: Open Sans; font-weight: 400; line-height: 1.7em;\"><img decoding=\"async\" loading=\"lazy\" class=\"img-responsive alignleft wp-image-296868 size-thumbnail\" src=\"https:\/\/ieeecs-media.computer.org\/wp-media\/2022\/08\/30224158\/Yauhen-Zaremba-Pandadoc-e1666035147284-150x150.jpg\" alt=\"Yauhen Zaremba\" width=\"150\" height=\"150\"\/>Yauhen Zaremba is the Director of Demand Generation at PandaDoc, all-in-one document management software for almost all document types including this PandaDoc flooring contract template. He\u2019s been a marketer for 10+ years, and for the last five years, he\u2019s been entirely focused on the electronic signature, proposal, and document management markets. Yauhen has experience speaking at niche conferences where he enjoys sharing his expertise with other curious marketers. And in his spare time, he is an avid fisherman and takes nearly 20 fishing trips every year.<\/p>\n<p>\u00a0<\/p>\n<div style=\"background-color: #d4f1f4; padding: 15px 15px 10px 15px;\">\n<p style=\"color: #454545; font-size: 18px; line-height: 1.7em;\"><strong>Disclaimer:<\/strong> The author is completely responsible for the content of this article. The opinions expressed are their own and do not represent IEEE\u2019s position nor that of the Computer Society nor its Leadership.<\/p>\n<\/div><\/div>\n<p>[ad_2]<br \/>\n<br \/><a href=\"https:\/\/www.computer.org\/publications\/tech-news\/build-your-career\/python-style-guide\/\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Writing code is one thing; writing beautiful, easily understandable code that others can use just as easily as you&hellip;<\/p>\n","protected":false},"author":1,"featured_media":506,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[474,232,475,86,476,2],"tags":[],"class_list":["post-505","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-programming","category-programming-language","category-python","category-style-guide","category-tech-news-post"],"_links":{"self":[{"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/posts\/505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/comments?post=505"}],"version-history":[{"count":0,"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/posts\/505\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/media\/506"}],"wp:attachment":[{"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/media?parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/categories?post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pc-keeper.tech\/index.php\/wp-json\/wp\/v2\/tags?post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}