{"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-02-06T08:31:09","modified_gmt":"2026-02-06T07:31:09","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":"<h2>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.<\/h2>\t\t\n\t\t<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><p>So the other day, trying to answer the question of why the following code displays two different results,<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"130\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.12-1024x167.png\" alt=\"java\" loading=\"lazy\" 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\" sizes=\"(max-width: 800px) 100vw, 800px\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\tI came across <a href=\"https:\/\/github.com\/pandas-dev\/pandas\/blob\/master\/pandas\/core\/series.py#L2083\">this.<\/a>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"149\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.24.18-1024x191.png\" alt=\"java\" loading=\"lazy\" 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\" sizes=\"(max-width: 800px) 100vw, 800px\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<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><p>&nbsp;<\/p><p>?Related articles:<\/p><table dir=\"ltr\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" data-sheets-root=\"1\"><colgroup><col width=\"656\"><\/colgroup><tbody><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Matplotlib: Master Data Visualization in Python &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/matplotlib-master-data-visualization-in-python\"><a href=\"https:\/\/liora.io\/en\/matplotlib-master-data-visualization-in-python\" target=\"_blank\" rel=\"noopener\">Matplotlib: Master Data Visualization in Python <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Python Crash Course: Get started &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/python-all-you-need-to-know\"><a href=\"https:\/\/liora.io\/en\/python-all-you-need-to-know\" target=\"_blank\" rel=\"noopener\">Python Crash Course: Get started <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Mastering Machine Learning in Python: Data-Driven Success &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/machine-learning-python-where-to-start\"><a href=\"https:\/\/liora.io\/en\/machine-learning-python-where-to-start\" target=\"_blank\" rel=\"noopener\">Mastering Machine Learning in Python: Data-Driven Success <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Python Programming for Beginners - Episode 3&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/python-programming-for-beginners-episode-3\"><a href=\"https:\/\/liora.io\/en\/python-programming-for-beginners-episode-3\" target=\"_blank\" rel=\"noopener\">Python Programming for Beginners &#8211; Episode 3<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Django: All about the Python web development framework&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/django-all-about-the-python-web-development-framework\"><a href=\"https:\/\/liora.io\/en\/django-all-about-the-python-web-development-framework\" target=\"_blank\" rel=\"noopener\">Django: All about the Python web development framework<\/a><\/td><\/tr><\/tbody><\/table>\t\t\n\t\t\t<h2>Dynamic typing vs. static typing<\/h2>\t\t\n\t\t<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>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"66\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.26.16-1024x85.png\" alt=\"java\" loading=\"lazy\" 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\" sizes=\"(max-width: 800px) 100vw, 800px\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<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>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"200\" height=\"200\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/volcan.gif\" alt=\"volcan explosion\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption>Me coding in Java &#8211; Allegory<\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>Python&#8217;s ease of use can also be a source of confusion.<\/p><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><p>But one of the features offered by Python allows us to give the illusion of static typing: annotations.<\/p>\t\t\n\t\t\t<h2>Annotations<\/h2>\t\t\n\t\t<p>In Python, you can use annotations to add information about the code you are writing. For example, I can write:<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"127\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.28-1024x162.png\" alt=\"java\" loading=\"lazy\" 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\" sizes=\"(max-width: 800px) 100vw, 800px\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<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>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"47\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.32-1024x60.png\" alt=\"java\" loading=\"lazy\" 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\" sizes=\"(max-width: 800px) 100vw, 800px\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<p>which refers:<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"41\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.38.png\" alt=\"java\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<p>However, I haven&#8217;t changed the internal structure of Python. Worse still, it&#8217;s useless.<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"43\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.44-1024x55.png\" alt=\"java\" loading=\"lazy\" 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\" sizes=\"(max-width: 800px) 100vw, 800px\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<p>and<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"72\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.43.50-1024x92.png\" alt=\"java\" loading=\"lazy\" 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\" sizes=\"(max-width: 800px) 100vw, 800px\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<p>work very well&#8230;<\/p><p>So what&#8217;s going on? Apart from the (relative) aesthetic aspect, is it totally useless?<\/p><p>Well, yes and no&#8230;<\/p><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><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>\t\t\n\t\t\t<h2>MyPy<\/h2>\t\t\n\t\t<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><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>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"300\" height=\"230\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2020\/05\/Capture-d\u2019e\u0301cran-2020-05-22-a\u0300-14.44.01-300x230.png\" alt=\"java\" loading=\"lazy\" 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\" sizes=\"(max-width: 300px) 100vw, 300px\">\t\t\t\t\t\t\t\t\t\t\t<figcaption>Static typing,  circa 2 ans et demi, colorized\n<\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<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><p>And even though I know that annotations are like good resolutions &#8211; you tell yourself you&#8217;re going to use them and two functions later, it&#8217;s forgotten &#8211; I at least wanted to tell you about my discovery.<\/p><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>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/appointment\">Book an appointment<\/a><\/div><\/div>\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":1,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/177766\/revisions"}],"predecessor-version":[{"id":206062,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/177766\/revisions\/206062"}],"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}]}}