{"id":177766,"date":"2024-02-08T10:18:29","date_gmt":"2024-02-08T09:18:29","guid":{"rendered":"https:\/\/liora.io\/en\/?p=177766"},"modified":"2026-08-09T19:38:26","modified_gmt":"2026-08-09T18:38:26","slug":"variable-types-in-python-how-about-clarifying-the-dynamics","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/variable-types-in-python-how-about-clarifying-the-dynamics","title":{"rendered":"Variable types in Python: how about clarifying the dynamics?"},"content":{"rendered":"\n<p><strong>Python has recently established itself as one of the leading programming languages for Data Science: easy to learn for people with a statistician&#8217;s background, well supplied with libraries, capable of anything, it&#8217;s a faithful companion for the Data Scientist.<\/strong><\/p>\n\n\n<p>However, it&#8217;s possible to do Data Science at a fairly high level without seeing all its particularities: how many Data Scientists use decorators? Is Python multi-threaded or multi-core?<\/p>\n\n\n<p>So the other day, trying to answer the question of why the following code displays two different results,<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:560px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"java\" decoding=\"async\" height=\"130\" loading=\"lazy\" sizes=\"(max-width: 800px) 100vw, 800px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.12-1024x167.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.12-1024x167.png 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.12-300x49.png 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.12-768x125.png 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.12.png 1200w\" style=\"width:560px;max-width:100%;height:auto\" width=\"800\"\/><\/figure>\n\n\n<p>I came across <a href=\"https:\/\/github.com\/pandas-dev\/pandas\/blob\/master\/pandas\/core\/series.py#L2083\">this.<\/a><\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:560px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"java\" decoding=\"async\" height=\"149\" loading=\"lazy\" sizes=\"(max-width: 800px) 100vw, 800px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.18-1024x191.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.18-1024x191.png 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.18-300x56.png 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.18-768x143.png 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.18.png 1190w\" style=\"width:560px;max-width:100%;height:auto\" width=\"800\"\/><\/figure>\n\n\n<p>For those who, like me, have already worked with Java, this may evoke memories (or even wounds), but for an inexperienced Python user, it may come as a surprise.<\/p>\n\n\n<div class=\"wp-block-group has-background has-global-padding is-layout-constrained wp-container-core-group-is-layout-7441ce17 wp-block-group-is-layout-constrained\" style=\"border-left-color:#ff5c43;border-left-width:4px;border-radius:12px;background-color:#fff5f2;margin-top:32px;margin-bottom:32px;padding-top:24px;padding-right:28px;padding-bottom:24px;padding-left:28px\">\n\n<p style=\"margin-top:0;margin-bottom:16px;font-size:clamp(14.642px, 0.915rem + ((1vw - 3.2px) * 0.575), 22px);font-style:normal;font-weight:600\">Related articles<\/p>\n\n\n<ul class=\"wp-block-list\" style=\"margin-top:0;margin-bottom:0;padding-left:20px\">\n<li><a href=\"https:\/\/liora.io\/en\/matplotlib-master-data-visualization-in-python\" rel=\"noopener\" target=\"_blank\">Matplotlib: Master Data Visualization in Python <\/a><\/li>\n<li><a href=\"https:\/\/liora.io\/en\/python-all-you-need-to-know\" rel=\"noopener\" target=\"_blank\">Python Crash Course: Get started <\/a><\/li>\n<li><a href=\"https:\/\/liora.io\/en\/machine-learning-python-where-to-start\" rel=\"noopener\" target=\"_blank\">Mastering Machine Learning in Python: Data-Driven Success <\/a><\/li>\n<li><a href=\"https:\/\/liora.io\/en\/python-programming-for-beginners-episode-3\" rel=\"noopener\" target=\"_blank\">Python Programming for Beginners ,  Episode 3<\/a><\/li>\n<li><a href=\"https:\/\/liora.io\/en\/django-all-about-the-python-web-development-framework\" rel=\"noopener\" target=\"_blank\">Django: All about the Python web development framework<\/a><\/li>\n<\/ul>\n\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"dynamic-typing-vs-static-typing\">Dynamic typing vs. static typing<\/h2>\n\n\n<p>Python is a dynamically typed language, meaning you can change the type of a variable. A statically typed language, on the other hand, forces you to define the type of a variable and keep it for the life of the variable.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:440px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"java\" decoding=\"async\" height=\"66\" loading=\"lazy\" sizes=\"(max-width: 800px) 100vw, 800px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.26.16-1024x85.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.26.16-1024x85.png 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.26.16-300x25.png 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.26.16-768x64.png 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.26.16.png 1200w\" style=\"width:440px;max-width:100%;height:auto\" width=\"800\"\/><\/figure>\n\n\n<p>This obviously makes Python very easy to use, since you don&#8217;t need to specify the type of a variable in advance, and you can easily change it. <a href=\"https:\/\/liora.io\/en\/java-fundamentals-explained-jdk-jre-and-jvm\">But trying to do that with Java&#8230;<\/a><\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:200px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"volcan explosion\" decoding=\"async\" height=\"200\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/volcan.gif\" style=\"width:200px;max-width:100%;height:auto\" width=\"200\"\/><figcaption class=\"wp-element-caption\">Me coding in Java ,  Allegory<\/figcaption><\/figure>\n\n\n<p>Python&#8217;s ease of use can also be a source of confusion.<\/p>\n\n\n<p>If you don&#8217;t know exactly what type of variable a function can accept, you may find it difficult to read someone else&#8217;s code.<\/p>\n\n\n<p>But one of the features offered by Python allows us to give the illusion of static typing: annotations.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"annotations\">Annotations<\/h2>\n\n\n<p>In Python, you can use annotations to add information about the code you are writing. For example, I can write:<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:560px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"java\" decoding=\"async\" height=\"127\" loading=\"lazy\" sizes=\"(max-width: 800px) 100vw, 800px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.28-1024x162.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.28-1024x162.png 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.28-300x48.png 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.28-768x122.png 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.28.png 1198w\" style=\"width:560px;max-width:100%;height:auto\" width=\"800\"\/><\/figure>\n\n\n<p>You can specify the nature of the arguments with these &#8220;:&#8221; and a string behind them, as well as the nature of the function&#8217;s output. These annotations can be accessed using:<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:440px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"java\" decoding=\"async\" height=\"47\" loading=\"lazy\" sizes=\"(max-width: 800px) 100vw, 800px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.32-1024x60.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.32-1024x60.png 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.32-300x17.png 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.32-768x45.png 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.32.png 1204w\" style=\"width:440px;max-width:100%;height:auto\" width=\"800\"\/><\/figure>\n\n\n<p>which refers:<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:440px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"java\" decoding=\"async\" height=\"41\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.38.png\" style=\"width:440px;max-width:100%;height:auto\" width=\"800\"\/><\/figure>\n\n\n<p>However, I haven&#8217;t changed the internal structure of Python. Worse still, it&#8217;s useless.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:440px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"java\" decoding=\"async\" height=\"43\" loading=\"lazy\" sizes=\"(max-width: 800px) 100vw, 800px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.44-1024x55.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.44-1024x55.png 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.44-300x16.png 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.44-768x42.png 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.44.png 1184w\" style=\"width:440px;max-width:100%;height:auto\" width=\"800\"\/><\/figure>\n\n\n<p>and<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:440px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"java\" decoding=\"async\" height=\"72\" loading=\"lazy\" sizes=\"(max-width: 800px) 100vw, 800px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.50-1024x92.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.50-1024x92.png 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.50-300x27.png 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.50-768x69.png 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.50.png 1202w\" style=\"width:440px;max-width:100%;height:auto\" width=\"800\"\/><\/figure>\n\n\n<p>work very well&#8230;<\/p>\n\n\n<p>So what&#8217;s going on? Apart from the (relative) aesthetic aspect, is it totally useless?<\/p>\n\n\n<p>Well, yes and no&#8230;<\/p>\n\n\n<p>Indeed, these annotations are only a means of obtaining information on the various arguments and outputs of your function, but the gain in clarity is not negligible when using the function.<\/p>\n\n\n<p>In fact, when working with several people, or more simply when sharing code, it can be very useful to be able to precisely identify the arguments and outputs of a function you&#8217;re using.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"mypy\">MyPy<\/h2>\n\n\n<p>But you can combine these annotations with another tool: MyPy. This is a library that hasn&#8217;t really understood how Python works and thinks that annotations are really type declarations.<\/p>\n\n\n<p>It allows you to check that you&#8217;re doing things correctly&#8230; that you&#8217;re putting squares inside squares, circles inside circles and triangles inside triangles.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:300px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"java\" decoding=\"async\" height=\"230\" loading=\"lazy\" sizes=\"(max-width: 300px) 100vw, 300px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.44.01-300x230.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.44.01-300x230.png 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.44.01.png 512w\" style=\"width:300px;max-width:100%;height:auto\" width=\"300\"\/><figcaption class=\"wp-element-caption\">Static typing,  circa 2 ans et demi, colorized\n<\/figcaption><\/figure>\n\n\n<p>The annotations should therefore be seen as an aid to development and code sharing, and MyPy as a guide to clean development. Admittedly, it doesn&#8217;t make much difference when you&#8217;re coding on a notebook and importing all your functions from sklearn or pandas. True, true static typing would speed up Python&#8217;s operation (since it would no longer need to infer the type of variables), but it can help you find your way around and debug too many lines of code.<\/p>\n\n\n<p>And even though I know that annotations are like good resolutions ,  you tell yourself you&#8217;re going to use them and two functions later, it&#8217;s forgotten ,  I at least wanted to tell you about my discovery.<\/p>\n\n\n<p>Did you like this article? Don&#8217;t hesitate to subscribe to our newsletter for data tips you won&#8217;t find anywhere else!<\/p>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-analyst\">Book an appointment<\/a><\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Python has recently established itself as one of the leading programming languages for Data Science: easy to learn for people with a statistician&#8217;s background, well supplied with libraries, capable of anything, it&#8217;s a faithful companion for the Data Scientist. However, it&#8217;s possible to do Data Science at a fairly high level without seeing all its [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":177768,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2434],"class_list":["post-177766","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-dev"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/177766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=177766"}],"version-history":[{"count":4,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/177766\/revisions"}],"predecessor-version":[{"id":210997,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/177766\/revisions\/210997"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/177768"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=177766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=177766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}