{"id":5665,"date":"2021-02-03T14:35:22","date_gmt":"2021-02-03T05:35:22","guid":{"rendered":"https:\/\/learningbox.co.jp\/?p=5665"},"modified":"2021-02-03T14:35:59","modified_gmt":"2021-02-03T05:35:59","slug":"blog_python-function-annotations","status":"publish","type":"post","link":"https:\/\/learningbox.co.jp\/en\/2021\/02\/03\/blog_python-function-annotations\/","title":{"rendered":"Type-aware programming in Python! Let's use type annotations"},"content":{"rendered":"<p>Hello. This is Tatsuno Information System Tokyo Team.<br \/>\nIn this article.<span class=\"yellowline\"><b>\"Type annotations introduced in Python 3.6<\/b><\/span>Here is some information about<br \/>\nSuddenly, when you write a program in Python, are you aware of the \"type\"? (There may be a few people who say \"I'm not aware of it\")<\/p>\n<p>In the case of dynamically typed languages such as Python, variables and other types are \"sort of\" made nice by the language.<br \/>\nIt's a good thing that you can write without being aware of it, but as the size of the source code grows<br \/>\n<b>\"What? Is the return value of this function a list or a dictionary? Or is it a dictionary?<\/b><br \/>\n<b>\"Is this property of this library a string?<\/b><br \/>\nYou'll spend a lot of time in non-coding areas like<\/p>\n<p>This is where the \"type annotations\" introduced in Python 3.6 come in.<br \/>\nAlthough this feature is already implemented in other dynamically typed languages (e.g. PHP), Python also allows variables to be commented about their type.<\/p>\n<ul class=\"mokuji\">\n<li>1. How to use the code<\/li>\n<li>2. Notes on using annotations<\/li>\n<li>3. At the end.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>How to use the code<\/h2>\n<p>Let's see how to use it with actual code.<\/p>\n<h3>variable annotation<\/h3>\n<p>Variable annotations are a way to explicitly write the type of a variable in Python.<br \/>\nFirst, here is a simple Hello world code.<\/p>\n<pre>str1 = 'Hello world!\r\nprint(str1)\r\n<\/pre>\n<p>Variable str1 is a string type (str type). At this level, type annotation is not necessary, but we dare to use type annotation.<\/p>\n<pre>str1 :str = 'Hello world!\r\nprint(str1)\r\n<\/pre>\n<p>in this way <code>Variable name :Variable type<\/code>You can specify \"which type the variable is\" by writing<br \/>\n&nbsp;<\/p>\n<h3>function annotation<\/h3>\n<p>We just added type annotations to variables, but you can also annotate function return values and arguments. (Function Annotations)<\/p>\n<p>You can use it to improve your development efficiency. Especially, if you add return type annotation, you can easily check the return type on the editor such as Visual Studio Code.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-5667\" src=\"https:\/\/tatsuno-system.co.jp\/wp-content\/uploads\/2021\/02\/image1-300x106.png\" alt=\"\" width=\"300\" height=\"106\" srcset=\"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2021\/02\/image1-300x106.png 300w, https:\/\/learningbox.co.jp\/wp-content\/uploads\/2021\/02\/image1.png 528w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p class=\"well\"><b>\u25bc How to write function annotations<\/b><br \/>\n1. The type expected for the argument of a function is indicated by appending a colon: after the argument.<br \/>\n2. The type expected for the return value is indicated by an arrow-&gt; after the closing parenthesis of the argument.\n<\/p>\n<p>Now let's look at some examples that might be useful in practice.<\/p>\n<pre>import datetime\r\ndef get_todays_date() -&gt; str:\r\n  dt_now = datetime.datetime.now()\r\n  date :str = dt_now.strftime(\"%m month %d day\")\r\n  return date\r\n\r\ntoday = get_todays_date()\r\nprint(\"Today is\", today, \"is\")\r\n<\/pre>\n<p>In the above program, it is not possible to distinguish whether the function get_todays_date returns as a datetime object or as a str type.<br \/>\nSo, we can use function annotations and<code> -&gt; str <\/code> you can see at a glance that the return value of get_todays_date is of type str. I think you can see it much better now.<br \/>\n&nbsp;<\/p>\n<h2>Points to note when using annotations<\/h2>\n<p>Type annotations are useful, but there are caveats.<br \/>\nFirst, you must have Python 3.6 or higher in use. If you are unable to upgrade your system or package due to dependency issues, you cannot use this feature.<br \/>\nAlso, unlike other languages, type annotations are implemented as comments, so even if the actual type and the type of the annotation are different, the program will still work if the syntax is correct. Therefore, there is a possibility that the misalignment will occur when refactoring. Be careful not to forget to fix the annotations.<br \/>\nThis is a brief explanation of how to use type annotations in Python.<br \/>\n&nbsp;<\/p>\n<h2>at the end<\/h2>\n<p><span class=\"yellowline\"><b>A program is like a piece of writing, it needs to be \"easy to read\".<\/b><\/span><\/p>\n<p>Properly adding explanations to your code will not only make it easier to read, but will also make it more valuable.<br \/>\nComments are the most generic method, but in this article, we introduced the Variable Annotation and Function Annotation, which are useful for explaining functions.<\/p>","protected":false},"excerpt":{"rendered":"\u3053\u3093\u306b\u3061\u306f\u3002\u9f8d\u91ce\u60c5\u5831\u30b7\u30b9\u30c6\u30e0\u6771\u4eac\u30c1\u30fc\u30e0\u3067\u3059\u3002 \u4eca\u56de\u306e\u8a18\u4e8b\u3067\u306f\u3001\u300cPython3.6\u304b\u3089\u5c0e\u5165\u3055\u308c\u305f\u578b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7 [&hellip;]","protected":false},"author":1,"featured_media":5682,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","_wpcom_ai_launchpad_first_post":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[6,7,40],"tags":[61,60],"class_list":["post-5665","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-7","category-40","tag-function-annotations","tag-python"],"acf":[],"jetpack_shortlink":"https:\/\/wp.me\/pgMrZ4-1tn","jetpack_featured_media_url":"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2021\/02\/2021-02-03_10-36-46.jpg","_links":{"self":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/5665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/comments?post=5665"}],"version-history":[{"count":27,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/5665\/revisions"}],"predecessor-version":[{"id":5698,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/5665\/revisions\/5698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media\/5682"}],"wp:attachment":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media?parent=5665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/categories?post=5665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/tags?post=5665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}