{"id":1791,"date":"2019-04-05T07:33:39","date_gmt":"2019-04-05T06:33:39","guid":{"rendered":"https:\/\/rosetta.vn\/short\/2019\/04\/05\/understanding-the-underscore-_-of-python-hacker-noon\/"},"modified":"2019-04-05T07:33:39","modified_gmt":"2019-04-05T06:33:39","slug":"understanding-the-underscore-_-of-python-hacker-noon","status":"publish","type":"post","link":"https:\/\/rosetta.vn\/short\/2019\/04\/05\/understanding-the-underscore-_-of-python-hacker-noon\/","title":{"rendered":"Understanding the underscore( _ ) of Python \u2013 Hacker Noon"},"content":{"rendered":"<blockquote>\n<p name=\"7fe6\" id=\"7fe6\" class=\"graf graf--p graf-after--p\" style=\"margin: 21px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\">There are 5 cases for using the<span class=\"Apple-converted-space\">&nbsp;<\/span><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">underscore<\/em>in Python.<\/p>\n<ol class=\"postList\" style=\"margin: 21px 0px 0px; padding: 0px; list-style: none none; counter-reset: post 0; color: rgba(0, 0, 0, 0.843); font-family: medium-content-sans-serif-font, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; font-size: 20px; line-height: 28px; background-color: rgb(255, 255, 255);\">\n<li name=\"dc17\" id=\"dc17\" class=\"graf graf--li graf-after--p\" style=\"margin-left: 30px; margin-bottom: 14px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em;\">For storing the value of last expression in interpreter.<\/li>\n<li name=\"d804\" id=\"d804\" class=\"graf graf--li graf-after--li\" style=\"margin-left: 30px; margin-bottom: 14px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em;\">For ignoring the specific values. (so-called \u201cI don\u2019t care\u201d)<\/li>\n<li name=\"af62\" id=\"af62\" class=\"graf graf--li graf-after--li\" style=\"margin-left: 30px; margin-bottom: 14px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em;\">To give special meanings and functions to name of vartiables or functions.<\/li>\n<li name=\"8962\" id=\"8962\" class=\"graf graf--li graf-after--li\" style=\"margin-left: 30px; margin-bottom: 14px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em;\">To use as \u2018Internationalization(i18n)\u2019 or \u2018Localization(l10n)\u2019 functions.<\/li>\n<li name=\"d139\" id=\"d139\" class=\"graf graf--li graf-after--li\" style=\"margin-left: 30px; margin-bottom: 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em;\">To separate the digits of number literal value.<\/li>\n<\/ol>\n<p name=\"35ba\" id=\"35ba\" class=\"graf graf--p graf-after--li\" style=\"margin: 21px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\">Let\u2019s look at each case.<\/p>\n<h3 name=\"acb4\" id=\"acb4\" class=\"graf graf--h3 graf-after--p\" style=\"font-family: medium-content-sans-serif-font, &quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, &quot;Lucida Sans&quot;, Geneva, Arial, sans-serif; letter-spacing: -0.015em; margin: 28px 0px 0px; color: rgba(0, 0, 0, 0.843); --baseline-multiplier:0.22; font-size: 30px; line-height: 1.15; background-color: rgb(255, 255, 255);\"><span class=\"markup--strong markup--h3-strong\" style=\"font-weight: inherit;\">When used in interpreter<\/span><\/h3>\n<p name=\"3a2c\" id=\"3a2c\" class=\"graf graf--p graf-after--h3\" style=\"margin: 8px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\">The python interpreter stores the last expression value to the special variable called \u2018_\u2019. This feature has been used in standard CPython interpreter first and you could use it in other Python interpreters too.<\/p>\n<pre name=\"d5a4\" id=\"d5a4\" class=\"graf graf--pre graf-after--p\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 35px; margin-bottom: 0px; padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">&gt;&gt;&gt; 10 \n10 \n&gt;&gt;&gt; _ \n10 \n&gt;&gt;&gt; _ * 3 \n30 \n&gt;&gt;&gt; _ * 20 \n600<\/pre>\n<h3 name=\"932e\" id=\"932e\" class=\"graf graf--h3 graf-after--pre\" style=\"font-family: medium-content-sans-serif-font, &quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, &quot;Lucida Sans&quot;, Geneva, Arial, sans-serif; letter-spacing: -0.015em; margin: 45px 0px 0px; color: rgba(0, 0, 0, 0.843); --baseline-multiplier:0.22; font-size: 30px; line-height: 1.15; background-color: rgb(255, 255, 255);\"><span class=\"markup--strong markup--h3-strong\" style=\"font-weight: inherit;\">For Ignoring the&nbsp;values<\/span><\/h3>\n<p name=\"9713\" id=\"9713\" class=\"graf graf--p graf-after--h3\" style=\"margin: 8px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\">The<span class=\"Apple-converted-space\">&nbsp;<\/span><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">underscore<\/em><span class=\"Apple-converted-space\">&nbsp;<\/span>is also used for ignoring the specific values. If you don\u2019t need the specific values or the values are not used, just assign the values to<span class=\"Apple-converted-space\">&nbsp;<\/span><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">underscore<\/em>.<\/p>\n<pre name=\"9708\" id=\"9708\" class=\"graf graf--pre graf-after--p\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 35px; margin-bottom: 0px; padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\"><em class=\"markup--em markup--pre-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\"># Ignore a value when unpacking\n<\/em>x, _, y = (1, 2, 3) <em class=\"markup--em markup--pre-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\"># x = 1, y = 3<\/em><\/pre>\n<pre name=\"1519\" id=\"1519\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\"><em class=\"markup--em markup--pre-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\"># Ignore the multiple values. It is called \"Extended Unpacking\" which is available in only Python 3.x<\/em>\nx, *_, y = (1, 2, 3, 4, 5) <em class=\"markup--em markup--pre-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\"># x = 1, y = 5<\/em><\/pre>\n<pre name=\"226b\" id=\"226b\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\"><em class=\"markup--em markup--pre-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\"># Ignore the index\n<\/em>for _ in range(10):     \n    do_something()<\/pre>\n<pre name=\"3986\" id=\"3986\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\"><em class=\"markup--em markup--pre-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\"># Ignore a value of specific location<\/em>\nfor _, val in list_of_tuple:\n    do_something()<\/pre>\n<h3 name=\"a31e\" id=\"a31e\" class=\"graf graf--h3 graf-after--pre\" style=\"font-family: medium-content-sans-serif-font, &quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, &quot;Lucida Sans&quot;, Geneva, Arial, sans-serif; letter-spacing: -0.015em; margin: 45px 0px 0px; color: rgba(0, 0, 0, 0.843); --baseline-multiplier:0.22; font-size: 30px; line-height: 1.15; background-color: rgb(255, 255, 255);\"><span class=\"markup--strong markup--h3-strong\" style=\"font-weight: inherit;\">Give special meanings to name of variables and functions<\/span><\/h3>\n<p name=\"57c3\" id=\"57c3\" class=\"graf graf--p graf-after--h3\" style=\"margin: 8px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\">The<span class=\"Apple-converted-space\">&nbsp;<\/span><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">underscore<\/em><span class=\"Apple-converted-space\">&nbsp;<\/span>may be most used in \u2018naming\u2019. The PEP8 which is Python convention guideline introduces the following 4 naming cases.<\/p>\n<p name=\"374d\" id=\"374d\" class=\"graf graf--p graf-after--p\" style=\"margin: 21px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\"><span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\">_single_leading_underscore<br \/>\n<\/span>This convention is used for declaring<span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\"><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">private<\/em><\/span><span class=\"Apple-converted-space\">&nbsp;<\/span>variables, functions, methods and classes in a module. Anything with this convention are ignored in<span class=\"Apple-converted-space\">&nbsp;<\/span><code class=\"markup--code markup--p-code\" style=\"font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; padding: 3px 4px; margin: 0px 2px; background: rgba(0, 0, 0, 0.0470588);\">from module import *<\/code>.&nbsp;<br \/>\nHowever, of course, Python does not supports<span class=\"Apple-converted-space\">&nbsp;<\/span><span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\"><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">truly private,<span class=\"Apple-converted-space\">&nbsp;<\/span><\/em><\/span>so we can not force somethings private ones and also can call it directly from other modules. So sometimes we say it \u201cweak internal use indicator\u201d.<\/p>\n<pre name=\"23a8\" id=\"23a8\" class=\"graf graf--pre graf-after--p\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 35px; margin-bottom: 0px; padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">_internal_name = 'one_nodule' # private variable\n_internal_version = '1.0' # private variable<\/pre>\n<pre name=\"aced\" id=\"aced\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">\nclass _Base: # private class\n    _hidden_factor = 2 # private variable<\/pre>\n<pre name=\"59cd\" id=\"59cd\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">    def __init__(self, price):\n        self._price = price<\/pre>\n<pre name=\"bf65\" id=\"bf65\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">    def _double_price(self): # private method\n        return self._price * self._hidden_factor<\/pre>\n<pre name=\"5a33\" id=\"5a33\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">    def get_double_price(self):\n        return self._double_price()<\/pre>\n<p name=\"698c\" id=\"698c\" class=\"graf graf--p graf-after--pre\" style=\"margin: 30px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\"><span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\">single_trailing_underscore_<br \/>\n<\/span>This convention could be used for avoiding conflict with Python keywords or built-ins. You might not use it often.<\/p>\n<pre name=\"1e5f\" id=\"1e5f\" class=\"graf graf--pre graf-after--p\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 35px; margin-bottom: 0px; padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">Tkinter.Toplevel(master, class_='ClassName') # Avoid conflict with 'class' keyword<\/pre>\n<pre name=\"543c\" id=\"543c\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">list_ = List.objects.get(1) # Avoid conflict with 'list' built-in type<\/pre>\n<p name=\"7d3d\" id=\"7d3d\" class=\"graf graf--p graf-after--pre\" style=\"margin: 30px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\"><span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\">__double_leading_underscore<br \/>\n<\/span>This is about syntax rather than a convention.<span class=\"Apple-converted-space\">&nbsp;<\/span><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">double underscore<span class=\"Apple-converted-space\">&nbsp;<\/span><\/em>will<em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\"><\/em>mangle the attribute names of a class to avoid conflicts of attribute names between classes. (so-called \u201cmangling\u201d that means that the compiler or interpreter modify the variables or function names with some rules, not use as it is)&nbsp;<br \/>\nThe mangling rule of Python is adding the \u201c_ClassName\u201d to front of attribute names are declared with<span class=\"Apple-converted-space\">&nbsp;<\/span><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">double underscore.&nbsp;<br \/>\n<\/em>That is, if you write method named \u201c__method\u201d in a class, the name will be mangled in \u201c_ClassName__method\u201d form.<\/p>\n<pre name=\"eeeb\" id=\"eeeb\" class=\"graf graf--pre graf-after--p\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 35px; margin-bottom: 0px; padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">class A:\n    def _single_method(self):\n        pass<\/pre>\n<pre name=\"cedc\" id=\"cedc\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">    def __double_method(self): # for mangling\n        pass<\/pre>\n<pre name=\"a38d\" id=\"a38d\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">class B(A):\n    def __double_method(self): # for mangling\n        pass<\/pre>\n<p name=\"dedc\" id=\"dedc\" class=\"graf graf--p graf-after--pre\" style=\"margin: 30px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\">Because of the attributes named with<em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">double underscore<span class=\"Apple-converted-space\">&nbsp;<\/span><\/em>will be mangled like above, we can not access it with \u201cClassName.__method\u201d. Sometimes, some people use it as like real private ones using these features, but it is not for private and not recommended for that. For more details, read<span class=\"Apple-converted-space\">&nbsp;<\/span><a href=\"http:\/\/python.net\/~goodger\/projects\/pycon\/2007\/idiomatic\/handout.html#naming\" data-href=\"http:\/\/python.net\/~goodger\/projects\/pycon\/2007\/idiomatic\/handout.html#naming\" class=\"markup--anchor markup--p-anchor\" rel=\"noopener noreferrer\" target=\"_blank\" style=\"color: inherit; -webkit-tap-highlight-color: rgba(0, 0, 0, 0.54); background-image: url(&quot;data:image\/svg+xml;utf8,<svg preserveAspectRatio=\\&quot;none\\&quot; viewBox=\\&quot;0 0 1 1\\&quot; xmlns=\\&quot;http:\/\/www.w3.org\/2000\/svg\\&quot;>\t<line x1=\\&quot;0\\&quot; y1=\\&quot;0\\&quot; x2=\\&quot;1\\&quot; y2=\\&quot;1\\&quot; stroke=\\&quot;currentColor\\&quot; \/><\/svg>&quot;); background-color: transparent; background-size: 1px 1px; background-position: 0px calc(1em + 1px); background-repeat: repeat-x;&#8221;>Python Naming<\/a>.<\/p>\n<p name=\"965a\" id=\"965a\" class=\"graf graf--p graf-after--p\" style=\"margin: 21px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\"><span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\">__double_leading_and_trailing_underscore__<br \/>\n<\/span>This convention is used for special variables or methods (so-called \u201cmagic method\u201d) such as<code class=\"markup--code markup--p-code\" style=\"font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; padding: 3px 4px; margin: 0px 2px; background: rgba(0, 0, 0, 0.0470588);\">__init__<\/code>,<span class=\"Apple-converted-space\">&nbsp;<\/span><code class=\"markup--code markup--p-code\" style=\"font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; padding: 3px 4px; margin: 0px 2px; background: rgba(0, 0, 0, 0.0470588);\">__len__<\/code>. These methods provides special syntactic features or does special things. For example,<span class=\"Apple-converted-space\">&nbsp;<\/span><code class=\"markup--code markup--p-code\" style=\"font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; padding: 3px 4px; margin: 0px 2px; background: rgba(0, 0, 0, 0.0470588);\">__file__<\/code><span class=\"Apple-converted-space\">&nbsp;<\/span>indicates the location of Python file,<span class=\"Apple-converted-space\">&nbsp;<\/span><code class=\"markup--code markup--p-code\" style=\"font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; padding: 3px 4px; margin: 0px 2px; background: rgba(0, 0, 0, 0.0470588);\">__eq__<\/code><span class=\"Apple-converted-space\">&nbsp;<\/span>is executed when<span class=\"Apple-converted-space\">&nbsp;<\/span><code class=\"markup--code markup--p-code\" style=\"font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; padding: 3px 4px; margin: 0px 2px; background: rgba(0, 0, 0, 0.0470588);\">a == b<\/code><span class=\"Apple-converted-space\">&nbsp;<\/span>expression is excuted.&nbsp;<br \/>\nA user of course can make custom special method, it is very rare case, but often might modify the some built-in special methods. (e.g. You should initialize the class with<span class=\"Apple-converted-space\">&nbsp;<\/span><code class=\"markup--code markup--p-code\" style=\"font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; padding: 3px 4px; margin: 0px 2px; background: rgba(0, 0, 0, 0.0470588);\">__init__<\/code><span class=\"Apple-converted-space\">&nbsp;<\/span>that will be executed at first when a instance of class is created.)<\/p>\n<pre name=\"55cd\" id=\"55cd\" class=\"graf graf--pre graf-after--p\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 35px; margin-bottom: 0px; padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">class A:\n    def __init__(self, a): # use special method '__init__' for initializing\n        self.a = a<\/pre>\n<pre name=\"02ad\" id=\"02ad\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">    def __custom__(self): # custom special method. you might almost do not use it\n        pass<\/pre>\n<h3 name=\"ad6f\" id=\"ad6f\" class=\"graf graf--h3 graf-after--pre\" style=\"font-family: medium-content-sans-serif-font, &quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, &quot;Lucida Sans&quot;, Geneva, Arial, sans-serif; letter-spacing: -0.015em; margin: 45px 0px 0px; color: rgba(0, 0, 0, 0.843); --baseline-multiplier:0.22; font-size: 30px; line-height: 1.15; background-color: rgb(255, 255, 255);\">As Internationalization(i18n)\/Localization(l10n) functions<\/h3>\n<p name=\"652b\" id=\"652b\" class=\"graf graf--p graf-after--h3\" style=\"margin: 8px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\">It is just convention, does not have any syntactic functions. That is, the underscore does not means<span class=\"Apple-converted-space\">&nbsp;<\/span><span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\">i18n\/l10n<\/span>, and it is just a convention that binds the<span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\">i18n\/l10n<\/span><span class=\"Apple-converted-space\">&nbsp;<\/span>to<span class=\"Apple-converted-space\">&nbsp;<\/span><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">underscore<\/em><span class=\"Apple-converted-space\">&nbsp;<\/span>variable has been from C convention.<br \/>\nThe built-in library<span class=\"Apple-converted-space\">&nbsp;<\/span><code class=\"markup--code markup--p-code\" style=\"font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; padding: 3px 4px; margin: 0px 2px; background: rgba(0, 0, 0, 0.0470588);\">gettext<\/code><span class=\"Apple-converted-space\">&nbsp;<\/span>which is for<span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\">i18n\/l10n<span class=\"Apple-converted-space\">&nbsp;<\/span><\/span>uses this convention, and Django which is Python web framework supports<span class=\"Apple-converted-space\">&nbsp;<\/span><span class=\"markup--strong markup--p-strong\" style=\"font-weight: 700;\">i18n\/l10n<\/span><span class=\"Apple-converted-space\">&nbsp;<\/span>also introduces and uses this convention.<\/p>\n<pre name=\"c04f\" id=\"c04f\" class=\"graf graf--pre graf-after--p\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 35px; margin-bottom: 0px; padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\"># see official docs : <a href=\"https:\/\/docs.python.org\/3\/library\/gettext.html\" data-href=\"https:\/\/docs.python.org\/3\/library\/gettext.html\" class=\"markup--anchor markup--pre-anchor\" rel=\"noopener noreferrer\" target=\"_blank\" style=\"color: inherit; text-decoration-line: underline; -webkit-tap-highlight-color: rgba(0, 0, 0, 0.54); background: 0px 0px;\">https:\/\/docs.python.org\/3\/library\/gettext.html<\/a>\nimport gettext<\/pre>\n<pre name=\"ec2d\" id=\"ec2d\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">gettext.bindtextdomain('myapplication','\/path\/to\/my\/language\/directory')\ngettext.textdomain('myapplication')\n_ = gettext.gettext<\/pre>\n<pre name=\"5ca7\" id=\"5ca7\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\"># ...<\/pre>\n<pre name=\"a2d3\" id=\"a2d3\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\"><code class=\"markup--code markup--pre-code\" style=\"font-family: inherit; font-size: 1em;\">print(_('This is a translatable string.'))<\/code><\/pre>\n<h3 name=\"545e\" id=\"545e\" class=\"graf graf--h3 graf-after--pre\" style=\"font-family: medium-content-sans-serif-font, &quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, &quot;Lucida Sans&quot;, Geneva, Arial, sans-serif; letter-spacing: -0.015em; margin: 45px 0px 0px; color: rgba(0, 0, 0, 0.843); --baseline-multiplier:0.22; font-size: 30px; line-height: 1.15; background-color: rgb(255, 255, 255);\">To separate the digits of number literal&nbsp;value<\/h3>\n<p name=\"2656\" id=\"2656\" class=\"graf graf--p graf-after--h3\" style=\"margin: 8px 0px 0px; --baseline-multiplier:0.17; font-family: medium-content-serif-font, Georgia, Cambria, &quot;Times New Roman&quot;, Times, serif; font-size: 18px; line-height: 1.58; letter-spacing: -0.004em; color: rgba(0, 0, 0, 0.843); background-color: rgb(255, 255, 255);\">This feature was added in Python 3.6. It is used for separating digits of numbers using<span class=\"Apple-converted-space\">&nbsp;<\/span><em class=\"markup--em markup--p-em\" style=\"font-feature-settings: 'liga' 1, 'salt' 1;\">underscore<span class=\"Apple-converted-space\">&nbsp;<\/span><\/em>for readability.<\/p>\n<pre name=\"1355\" id=\"1355\" class=\"graf graf--pre graf-after--p\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 35px; margin-bottom: 0px; padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">dec_base = 1_000_000\nbin_base = 0b_1111_0000\nhex_base = 0x_1234_abcd<\/pre>\n<pre name=\"8bff\" id=\"8bff\" class=\"graf graf--pre graf-after--pre\" style=\"overflow: auto; font-family: Menlo, Monaco, &quot;Courier New&quot;, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 4px 20px 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.843); background: rgba(0, 0, 0, 0.047);\">print(dec_base) # 1000000\nprint(bin_base) # 240\nprint(hex_base) # 305441741<\/pre>\n<\/blockquote>\n<p><a href=\"https:\/\/hackernoon.com\/understanding-the-underscore-of-python-309d1a029edc?gi=a08387cb7318\">https:\/\/hackernoon.com\/understanding-the-underscore-of-python-309d1a029edc?gi=a08387cb7318<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are 5 cases for using the&nbsp;underscorein Python. For storing the value of last expression in interpreter. For ignoring the specific values. (so-called \u201cI don\u2019t care\u201d) To give special meanings and functions to name of vartiables or functions. To use<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"quote","meta":{"_mi_skip_tracking":false},"categories":[30,215],"tags":[47,222,1192],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p8jhJx-sT","_links":{"self":[{"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/posts\/1791"}],"collection":[{"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/comments?post=1791"}],"version-history":[{"count":0,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/posts\/1791\/revisions"}],"wp:attachment":[{"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/media?parent=1791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/categories?post=1791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/tags?post=1791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}