{"id":7316,"date":"2024-02-27T11:19:46","date_gmt":"2024-02-27T11:19:46","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7316"},"modified":"2024-02-27T11:23:49","modified_gmt":"2024-02-27T11:23:49","slug":"function-annotations-in-python","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/function-annotations-in-python\/","title":{"rendered":"Function Annotations in Python"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><h4>Table of Contents<\/h4><nav><ul><li ><a href=\"#what-are-future-annotations\">What are Future Annotations?<\/a><\/li><li ><a href=\"#what-is-the-syntax-of-the-future-annotations\">What is the syntax of the Future Annotations?<\/a><\/li><li ><a href=\"#what-are-the-examples-of-function-annotation-in-python\">What are the examples of Function annotation in Python?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#function-annotation-in-python-fa-qs\">Function Annotation in Python -FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Function annotations are random expressions that are written with the functions and won&#8217;t be evaluated in the compile time. Moreover, the future annotation won&#8217;t exist at the run time.  This article has listed in the future annotations in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-future-annotations\">What are Future Annotations?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Function Annotations are the arbitrary <a href=\"https:\/\/www.skillvertex.com\/blog\/python-positional-only-arguments\/\" data-type=\"post\" data-id=\"7303\">Python<\/a> expressions that will be associated with the various parts of functions. Hence, it won&#8217;t be checked at the compile time and doesn&#8217;t play any role during the existing time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Moreover, function annotations are mostly used by third-party<a href=\"https:\/\/www.skillvertex.com\/blog\/c-library-math-h-functions\/\" data-type=\"post\" data-id=\"2596\"> libraries<\/a> as it is known that the different libraries have different benefits from these function annotations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a.Future annotations with <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-find-length-of-string-in-java-python-cpp-javascript-sql-shell-script-mysql-oracle-and-perl\/\" data-type=\"post\" data-id=\"452\">strings <\/a>will work as messages at the compile time for describing the functionality of different methods, <a href=\"https:\/\/www.skillvertex.com\/blog\/how-many-classes-can-be-defined-in-a-single-program\/\" data-type=\"post\" data-id=\"2694\">classes,<\/a> and <a href=\"https:\/\/www.skillvertex.com\/blog\/python-variables\/\" data-type=\"post\" data-id=\"6932\">variables<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">b. <strong>[def fxn(a:&#8221;int&#8221;, b:&#8221;float&#8221;=5.0) -&gt; &#8220;int&#8221;]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the example provided above, these annotations will define the data types of the <a href=\"https:\/\/www.skillvertex.com\/blog\/what-are-the-limitations-of-parameters-in-tableau\/\" data-type=\"post\" data-id=\"3022\">parameters<\/a> as Python will support the dynamic type checking and it is not possible to <a href=\"https:\/\/www.skillvertex.com\/blog\/what-is-the-return-type-of-getchar-fgetc-and-getc\/\" data-type=\"post\" data-id=\"3597\">return type<\/a>. Thus, libraries will use these kinds of annotations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-syntax-of-the-future-annotations\">What is the syntax of the Future Annotations?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>a. Future Annotation for simple parameters<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def functionName(argumentName : expression):  \n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> Colons are used after the argument name and then we can write the expression after the colon. This expression will appear similar to any data type of the argument.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def fxn(var1 : expression1,var2:expression2:96):  \n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>b. Function annotations for excess parameters<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if it is required to use an arbitrary number of arguments as the function parameters with the same expression. Hence, it is possible to use the function annotation. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def fxn(*arg1: expression1,**arg2:expression2):  \n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>c. Function annotation for the nested parameters<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If it is required to pass the list in the function call it as the argument and thus, apply the function annotations on the individual elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def fxn((var1:expression,var2:expression),(var3:expression,var4:expression)):  <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>d. Function annotation for the return type:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The annotation of the return time will be operated with the help of the&#8217;&gt;&#8217; operator.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def fxn(var1:expression) -&gt; expression2:  <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-examples-of-function-annotation-in-python\">What are the examples of Function annotation in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The examples of function annotation in Python are given below:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_numbers(x: int, y: int) -&gt; int:\n    \"\"\"\n    Adds two numbers and returns the result.\n\n    :param x: The first number.\n    :param y: The second number.\n    :return: The sum of x and y.\n    \"\"\"\n    result = x + y\n    return result\n\n# Example usage:\nnum1 = 5\nnum2 = 7\nsum_result = add_numbers(num1, num2)\n\nprint(f\"The sum of {num1} and {num2} is: {sum_result}\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The sum of 5 and 7 is: 12\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>&nbsp;Using &#8216;__annotations__&#8217;<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The attribute __annotations__  will get every annotation in the function. It will return with the dictionary and has a pair of keys and values in which the key would act as an argument and the value will be in their expression.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def calculate_discount(original_price: float, discount_rate: float = 0.1) -&gt; float:\n    \"\"\"\n    Calculate the discounted price based on the original price and discount rate.\n\n    :param original_price: The original price of the item.\n    :param discount_rate: The discount rate (default is 10%).\n    :return: The discounted price.\n    \"\"\"\n    discounted_price = original_price - (original_price * discount_rate)\n    return discounted_price\n\n# Example usage:\nitem_price = 100.0\ndiscounted_price = calculate_discount(item_price)\n\nprint(f\"Original Price: ${item_price}\")\nprint(f\"Discounted Price: ${discounted_price}\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original Price: $100.0\nDiscounted Price: $90.0\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>b. Using the standard  Python module:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, there&#8217;s a module called pydoc that helps create documentation for Python code. While it doesn&#8217;t have a help() function to open a shell environment, you can still get information about function annotations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>c. Using the inspect module<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, there is another standard module which is known as inspect. This consists of information on a file, module, class, or object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import inspect\n\ndef example_function(x: int, y: str) -&gt; float:\n    return float(x)\n\n# Get information about the function using inspect\nfunction_info = inspect.getfullargspec(example_function)\n\n# Print the information\nprint(\"Function Name:\", example_function.__name__)\nprint(\"Parameters:\", function_info.args)\nprint(\"Default Values:\", function_info.defaults)\nprint(\"Annotations:\", example_function.__annotations__)\nprint(\"Docstring:\", example_function.__doc__)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function Name: example_function\nParameters: &#91;'x', 'y']\nDefault Values: None\nAnnotations: {'x': &lt;class 'int'&gt;, 'y': &lt;class 'str'&gt;, 'return': &lt;class 'float'&gt;}\nDocstring: None\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To conclude, this article has described the  Future Annotations in Python. It has also included the benefits of future annotations. This information can allow to improve skills and knowledge regarding the future annotation of Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"function-annotation-in-python-fa-qs\">Function Annotation in Python -FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1709030582509\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is a function annotation in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Function annotations will work for both parameters and return values, and are completely optional.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709030590294\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is an annotation in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In Python, annotations are like notes in the code that help give information about variables, what kind of data functions expect, and what they give back.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709030597531\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.What is function typing annotation in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A type annotation that is referred to as a type hint, is\u00a0also an optional notation that specifies the type of a parameter or function result.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Function annotations are random expressions that are written with the functions and won&#8217;t be evaluated in the compile time. Moreover, the future annotation won&#8217;t exist at the run time. This article has listed in the future annotations in Python. What are Future Annotations? Function Annotations are the arbitrary Python expressions that will be associated with &#8230; <a title=\"Function Annotations in Python\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/function-annotations-in-python\/\" aria-label=\"More on Function Annotations in Python\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7318,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[947,945,866],"class_list":["post-7316","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-function-annotation-in-python","tag-python-function","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7316"}],"version-history":[{"count":5,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7316\/revisions"}],"predecessor-version":[{"id":7322,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7316\/revisions\/7322"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7318"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}