{"id":1720,"date":"2024-04-19T20:25:48","date_gmt":"2024-04-19T14:55:48","guid":{"rendered":"https:\/\/geekpython.in\/?p=1720"},"modified":"2024-04-19T20:25:49","modified_gmt":"2024-04-19T14:55:49","slug":"type-hinting-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/type-hinting-in-python","title":{"rendered":"Python Type Hints: Functions, Return Values, Variable"},"content":{"rendered":"\n<p>Python is a dynamically typed language, meaning you do not need to specify the type of variables, parameters or return values. This is determined during program execution based on the values assigned to the variable or passed as the argument.<\/p>\n\n\n\n<p>Python introduced <strong>type hints<\/strong> or <strong>static typing<\/strong> with version 3.5, allowing developers to declare the data type of variables, parameters, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Type Hints?<\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >x: int = 5\ny: str = \"5\"<\/pre><\/div>\n\n\n\n<p>In this example, the variable <code>x<\/code> expects to be an integer value while the variable <code>y<\/code> expects to be a string value.<\/p>\n\n\n\n<p>This is called <strong>type hint<\/strong> or <strong>static typing<\/strong> where <strong>you specify the expected data type<\/strong> for a variable, parameter or return value of a function.<\/p>\n\n\n\n<p>Python has a different way of declaring type hints for the variables, return values, collections, etc.<\/p>\n\n\n\n<p>Consider the following example:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:1 decode:true \" >def circle(radius: float) -&gt; str:\n    area = 3.14 * radius ** 2\n    return f\"Area of circle: {area}\"<\/pre><\/div>\n\n\n\n<p>In this example, the function circle accepts an argument <code>radius<\/code> which is expected to be a float value as indicated by type hint <code>radius: float<\/code>, and the return value of this function is expected to be a string, as indicated by the <code>-&gt; str<\/code> hint.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Performing a Check<\/h2>\n\n\n\n<p>What if you pass the argument of a different type which is not expected? Consider the function below:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:1,4 decode:true \" >def date(dd: str, mm: str, yyyy: str) -&gt; str:\n    return f\"Current Date: {dd}-{mm}-{yyyy}\"\n\ncurr_date = date(22, 9, 2024)  # expected 'str' got 'int'\nprint(curr_date)<\/pre><\/div>\n\n\n\n<p>The function <code>date()<\/code> accepts three arguments and expects all of them to be a string value, however, integer values are supplied when the function is called.<\/p>\n\n\n\n<p>What do you think? Will this program throw an error? Well, it looks like but Python or specifically interpreter completely ignores the type hints as this is not its purpose.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >Current Date: 22-9-2024<\/pre><\/div>\n\n\n\n<p>The type of arguments was decided in the runtime that is why no error is thrown, however, you may see a warning in your IDE or code editor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Annotating Multiple Return Types<\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to annotate multiple return types for a single value of alternative types and multiple values of different types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Alternative Types for a Return Value<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >def cart(item: str) -&gt; str | None:\n    if item == \"\":\n        return None\n    return \"Item added in the cart\"<\/pre><\/div>\n\n\n\n<p>The function <code>cart()<\/code> accepts an argument item and returns <code>None<\/code> if it is not supplied, otherwise, it returns a string.<\/p>\n\n\n\n<p>To represent multiple return types for a function, the (<code>|<\/code>) pipe can be used. It means either <code>str<\/code> or <code>None<\/code>. So, when you call the function, it indicates that the return value of the function can be a <code>str<\/code> or <code>None<\/code>.<\/p>\n\n\n\n<p>You can also use <code>typing.Union<\/code> to accomplish the same task you&#8217;ve done using the (<code>|<\/code>).<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:3 decode:true \" >from typing import Union\n\ndef cart(item: str) -&gt; Union[None, str]:\n    if item == \"\":\n        return None\n    return \"Item added in the cart\"<\/pre><\/div>\n\n\n\n<p>The <code>Union[None, str]<\/code> is equivalent to <code>None | str<\/code>. <code>None | str<\/code> is a shorthand and it is a recommended way.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Return Values of Different Types<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >def cart(item: str, quantity: int) -&gt; dict[str, int] | None:\n    if item == \"\" or quantity == 0:\n        return None\n    return {item: quantity}<\/pre><\/div>\n\n\n\n<p>The function <code>cart()<\/code> has been updated to accept an additional argument <code>quantity<\/code> and returns <code>None<\/code> if any of the arguments are left empty, otherwise, it returns a dictionary containing a key (<code>str<\/code>) and value (<code>int<\/code>).<\/p>\n\n\n\n<p>If you look at the type hint for the return value, this time different types (<code>str<\/code> and <code>int<\/code>) are expected to return in a dictionary (<code>dict<\/code>).<\/p>\n\n\n\n<p>You can do this in the following way.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:3 decode:true \" >from typing import Union, Dict\n\ndef cart(item: str, quantity: int) -&gt; Union[Dict[str, int], None]:\n    if item == \"\" or quantity == 0:\n        return None\n    return {item: quantity}<\/pre><\/div>\n\n\n\n<p>You can use <code>Mapping<\/code> instead of <code>Dict<\/code> to represent the dictionary.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >from typing import Union, Mapping\n\ndef cart(item: str, quantity: int) -&gt; Union[Mapping[str, int], None]:\n    if item == \"\" or quantity == 0:\n        return None\n    return {item: quantity}<\/pre><\/div>\n\n\n\n<p>But all this seems a bit verbose so you can go for shorthand syntax.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Type Hinting Functions<\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:4 decode:true \" >from collections.abc import Callable\n\ndef apply_cart(\n        func: Callable[[str, int], dict[str, int]],\n        item: str,\n        quantity: int\n) -&gt; dict[str, int]:\n    return func(item, quantity)\n\ndef cart(item: str, quantity: int) -&gt; dict[str, int] | None:\n    if item == \"\" or quantity == 0:\n        return None\n    return {item: quantity}<\/pre><\/div>\n\n\n\n<p>The function <code>apply_cart()<\/code> accepts a callable object (which can be a function or any other callable object) and two arguments (<code>item<\/code> and <code>quantity<\/code>) and returns a dictionary containing the key and value.<\/p>\n\n\n\n<p>The <code>Callable<\/code> type hint provides a list of arguments (<code>[str, int]<\/code>) that the callable object accepts. In this case, <code>func()<\/code> expects strings and integers. Callable&#8217;s second parameter is the return value (<code>dict[str, int]<\/code>), which is a dictionary.<\/p>\n\n\n\n<p>The second function, <code>cart()<\/code>, is identical to the previous example and returns a dictionary if everything is correct.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >cart_item = apply_cart(cart, \"Mouse\", 2)\nprint(cart_item)\n--------------------\n{'Mouse': 2}<\/pre><\/div>\n\n\n\n<p>The <code>apply_cart()<\/code> function is invoked with the <code>cart<\/code> (a function) and two arguments (<code>'Mouse'<\/code>, <code>2<\/code>).<\/p>\n\n\n\n<p>The <code>apply_cart()<\/code> function calls the <code>cart()<\/code> function with the given arguments and returns a result.<\/p>\n\n\n\n<p>Here&#8217;s an optimisation, if you have many arguments of different types, you can use <strong>ellipsis<\/strong> (<code>...<\/code>) rather than passing different input types.<\/p>\n\n\n\n<p>The ellipsis literal (<code>...<\/code>) indicates that callable can accept any arbitrary list of arguments.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:4,7 decode:true \" >from collections.abc import Callable\nfrom typing import TypeVar, Any\n\nT = TypeVar(\"T\")\n\ndef apply_cart(\n        func: Callable[..., T],\n        *args: Any\n) -&gt; T:\n    return func(*args)<\/pre><\/div>\n\n\n\n<p>The <code>apply_cart()<\/code> function has been updated and now accepts an arbitrary list of arguments of any type (<code>[..., T]<\/code>) as well as variadic argument (<code>*args<\/code>) of any type (<code>Any<\/code>).<\/p>\n\n\n\n<p>The first parameter in the <code>Callable<\/code> is an ellipsis (<code>...<\/code>), suggesting that any arbitrary parameter list is permitted.<\/p>\n\n\n\n<p>The second parameter is a <strong>type variable<\/strong> (<code>T = TypeVar(\"T\")<\/code>) that can work with any type. It indicates that the callable can accept any type and return an element of that type.<\/p>\n\n\n\n<p>You can also use a <strong>parameter specification variable<\/strong> (<code>ParamSpec<\/code>) instead of an ellipsis to make callable objects accept any number of positional or keyword arguments.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:4,8 decode:true \" >from collections.abc import Callable\nfrom typing import TypeVar, ParamSpec\n\nP = ParamSpec(\"P\")\nT = TypeVar(\"T\")\n\ndef apply_cart(\n        func: Callable[P, T],\n        *args: P.args\n) -&gt; T:\n    return func(*args)<\/pre><\/div>\n\n\n\n<p>In this case, the first parameter of <code>Callable<\/code> is a <strong>parameter specification variable<\/strong> (<code>P = ParamSpec(\"P\")<\/code>) indicating an arbitrary list of arguments is acceptable. The second parameter (<code>T<\/code>) indicates that any type is acceptable.<\/p>\n\n\n\n<p>The function <code>apply_cart()<\/code> also accepts a variadic argument (<code>*args<\/code>) of type <code>P.args<\/code> that represents a tuple of any number and type of positional arguments. To annotate <code>**kwargs<\/code>, <code>P.kwargs<\/code> must be used that represent the mapping of keyword parameters to their values.<\/p>\n\n\n\n<p>Instead of using the ellipsis literal, you may now utilise <code>ParamSpec<\/code> and <code>TypeVar<\/code> to enable callable objects to accept any number of positional or keyword arguments of any type.<\/p>\n\n\n\n<p>The code is further updated by adding <code>**kwargs<\/code>, which allows the function to accept keyword arguments of arbitrary length.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:10,12 decode:true \">from collections.abc import Callable\nfrom typing import TypeVar, ParamSpec\n\nP = ParamSpec(\"P\")\nT = TypeVar(\"T\")\n\ndef apply_cart(\n        func: Callable[P, T],\n        *args: P.args,\n        **kwargs: P.kwargs\n) -&gt; T:\n    return func(*args, **kwargs)\n\n\ndef cart(item: str, quantity: int) -&gt; dict[str, int] | None:\n    if item == \"\" or quantity == 0:\n        return None\n    return {item: quantity}\n\ncart_item = apply_cart(cart, \"Mouse\", quantity=2)\nprint(cart_item)<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Type Hinting Iterables<\/h2>\n\n\n\n<p>Consider the following function that takes a list of names and sorts them in ascending order.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">def sort_student(names: list[str]) -&gt; None:\n    sorting = sorted(names)\n    for name in sorting:\n        print(name)\n\nn = [\"Gojo\", \"Yuta\", \"Yuji\", \"Megumi\"]\nsort_student(n)<\/pre><\/div>\n\n\n\n<p>The <code>list[str]<\/code> type is used to type hint the parameter <code>names<\/code> to indicate that it expects a list of strings.<\/p>\n\n\n\n<p>But what if you want <code>names<\/code> to be a type <strong>tuple<\/strong> or <strong>set<\/strong> instead? You need to refactor your code. In this case, you need to change the type in one location, however, this might be hard in complex and big projects.<\/p>\n\n\n\n<p>You can use the type <code>Iterable<\/code> to make the function accept any iterable object.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from collections.abc import Iterable\n\ndef sort_student(names: Iterable[str]) -&gt; None:\n    sorting = sorted(names)\n    for name in sorting:\n        print(name)\n\nn1 = (\"Gojo\", \"Yuta\", \"Yuji\", \"Megumi\") # No error\nn2 = [\"Gojo\", \"Yuta\", \"Yuji\", \"Megumi\"] # No error\nn3 = {\"Gojo\", \"Yuta\", \"Yuji\", \"Megumi\"} # No error<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Type Aliases for Better Readability<\/h2>\n\n\n\n<p>The complex type hints for callable objects like in the above case, arguments, and return values of the functions kind of feel cumbersome to write.<\/p>\n\n\n\n<p>Why can&#8217;t simplify the complex type by giving them an <strong>alias<\/strong>(name)? You can create an alias for the type in the same way you would create a variable.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">type ReturnCart = dict[str, int] | None\n\ndef cart(item: str, quantity: int) -&gt; ReturnCart:\n    if item == \"\" or quantity == 0:\n        return None\n    return {item: quantity}\n\ncart_item = apply_cart(cart, \"Mouse\", quantity=2)\nprint(cart_item)<\/pre><\/div>\n\n\n\n<p>In the above example, an alias <code>ReturnCart<\/code> is created and contains the type of return value (<code>dict[str, int] | None<\/code>).<\/p>\n\n\n\n<p>The alias is defined with the <a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/reference\/lexical_analysis.html#soft-keywords\">soft keyword<\/a> <a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#type\"><code>type<\/code><\/a>, which creates an instance of <a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/typing.html#typing.TypeAliasType\"><code>TypeAliasType<\/code><\/a>. This keyword was added in Python 3.12. If you are using an older version of Python, you can choose an alternate method.<\/p>\n\n\n\n<p>Type aliases can also be created like you would create a variable using a simple assignment.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">ReturnCart = dict[str, int] | None<\/pre><\/div>\n\n\n\n<p>To clarify, you can mark it with <code>TypeAlias<\/code> to explicitly show this as a type alias, not a simple variable.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from typing import TypeAlias\n\nReturnCart: TypeAlias = dict[str, int] | None<\/pre><\/div>\n\n\n\n<p>The methods described above will assist you in simplifying complex types, and the key benefit is that you will only need to edit the type in one spot, eliminating the need for refactoring.<\/p>\n\n\n\n<p>Another advantage of using type aliases is that they can be reused in other portions of your code, which improves code readability.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Type Checker Tools<\/h2>\n\n\n\n<p>Python entirely ignores type hints and determines the type of variables, function arguments, and return values at runtime.<\/p>\n\n\n\n<p><a href=\"https:\/\/mypy.readthedocs.io\/en\/stable\/getting_started.html#\" target=\"_blank\" rel=\"noreferrer noopener\">Mypy<\/a>, a popular third-party tool, can enforce type-checking in Python. Since it is a third-party tool, you must install it using the following command.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">python -m pip install mypy<\/pre><\/div>\n\n\n\n<p>This will provide you access to the <code>mypy<\/code> command to type-check your program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Static Type Checking Using Mypy<\/h3>\n\n\n\n<p>The function <code>sort_student()<\/code> takes an iterable object and sorts the student names in ascending order. Save this function to a file, such as <code>student.py<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"student.py\"># student.py\nfrom collections.abc import Iterable\n\ndef sort_student(names: Iterable[str]) -&gt; None:\n    sorting = sorted(names)\n    for name in sorting:\n        print(name)\n\nn1 = (\"Gojo\", \"Yuta\", \"Yuji\", \"Megumi\")\nsort_student(n1)<\/pre><\/div>\n\n\n\n<p>To type check your program using <code>mypy<\/code>, enter the command <code>mypy<\/code> followed by the file name you want to check in the terminal.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">&gt; mypy student.py \nSuccess: no issues found in 1 source file<\/pre><\/div>\n\n\n\n<p>The type checker identified no errors. One thing you may have noticed is that the code contains the function call (<code>sort_student(n1)<\/code>), which should have created the output, but there was none in the terminal except the message created by <code>mypy<\/code>.<\/p>\n\n\n\n<p>This means that <code>mypy<\/code> does not execute your code, instead, it examines if the values match the expected type based on the type hints.<\/p>\n\n\n\n<p>What if you made a mistake and the actual values do not match the expected type?<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"student.py\"># student.py\ndef sort_student(names: list[str]) -&gt; None:\n    sorting = sorted(names)\n    for name in sorting:\n        print(name)\n\nn1 = (\"Gojo\", \"Yuta\", \"Yuji\", \"Megumi\")\nsort_student(n1)<\/pre><\/div>\n\n\n\n<p>This function expects a list of strings but the tuple was passed. If you now check your code, it will generate the following error.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">&gt; mypy student.py\nstudent.py:8: error: Argument 1 to \"sort_student\" has incompatible type \"tuple[str, str, str, str]\"; expected \"list[str]\"  [arg-type]\nFound 1 error in 1 file (checked 1 source file)<\/pre><\/div>\n\n\n\n<p>Mypy has identified the error on line number 8 saying that the argument passed to <code>sort_student()<\/code> has an incompatible type <code>tuple[str, str, str, str]<\/code>, it is expected to be a <code>list[str]<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/mypy.readthedocs.io\/en\/stable\/cheat_sheet_py3.html\" target=\"_blank\" rel=\"noreferrer noopener\">Type cheat sheet<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p><strong>Type hints<\/strong> are optional in Python but they can be used to make the code more readable and using type hints can avoid possible bugs in your code.<\/p>\n\n\n\n<p>In this article, you&#8217;ve learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Implementing type hints<\/li>\n\n\n\n<li>Alternative types for single data and different types for multiple values using pipe (<code>|<\/code>) and <code>Union<\/code><\/li>\n\n\n\n<li><code>Callable<\/code> type for type hinting functions<\/li>\n\n\n\n<li>Ellipsis (<code>...<\/code>) and <code>TypeVar<\/code> in <code>Callable<\/code> type to make a callable object accept an arbitrary list of arguments of any type<\/li>\n\n\n\n<li><code>ParamSpec<\/code> instead of an ellipsis with <code>TypeVar<\/code> in Callable type to make a good combination for a callable object accepting an arbitrary list of arguments of any type<\/li>\n\n\n\n<li><code>Iterable<\/code> type for type hinting iterable objects<\/li>\n\n\n\n<li>Type aliases to simplify complex type<\/li>\n\n\n\n<li><strong>Mypy<\/strong> for type checking in Python<\/li>\n<\/ul>\n\n\n\n<p>There is <a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/typing.html#\">much more<\/a> you can do with type hints in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/positional-and-keyword-arguments-in-python\">Best Practices: Positional and Keyword Arguments in Python<\/a><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/decorators-in-python\">Decorators in Python and How to Create a Custom Decorator<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/build-websocket-server-and-client-using-python\">Create a WebSocket Server and Client in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/mysql-database-in-python\">Create and Interact with MySQL Database in Python<\/a><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/asterisk-in-python\">Understanding the Different Uses of the Asterisk(*) in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/yield-keyword-in-python\">Yield Keyword in Python with Examples<\/a>?<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is a dynamically typed language, meaning you do not need to specify the type of variables, parameters or return values. This is determined during program execution based on the values assigned to the variable or passed as the argument. Python introduced type hints or static typing with version 3.5, allowing developers to declare the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1724,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[2,71],"tags":[31,78],"class_list":["post-1720","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-misc","tag-python3","tag-type-hint","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1720"}],"version-history":[{"count":4,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1720\/revisions"}],"predecessor-version":[{"id":1725,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1720\/revisions\/1725"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1724"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}