Understanding the underscore( _ ) of PythonI’m not a native speaker. Sorry for my english. Please understand. The underscore (_) is special in Python. While the underscore (_) is used for just snake-case variables and functions in most languages (Of course, not for all), but it has special meanings in Python. If you are python programmer, This post will explain the about when and how use the underscore (_) and help you understand it. There are 5 cases for using the underscore in Python.
Let’s look at each case. When used in interpreterThe python interpreter stores the last expression value to the special variable called ‘_’. This feature has been used in standard CPython interpreter first and you could use it in other Python interpreters too. >>> 10 For Ignoring the valuesThe underscore is also used for ignoring the specific values. If you don’t need the specific values or the values are not used, just assign the values to underscore. # Ignore a value when unpacking # Ignore the multiple values. It is called "Extended Unpacking" which is available in only Python 3.x # Ignore the index # Ignore a value of specific location Give special meanings to name of variables and functionsThe underscore may be most used in ‘naming’. The PEP8 which is Python convention guideline introduces the following 4 naming cases. _single_leading_underscore _internal_name = 'one_nodule' # private variable
def __init__(self, price): def _double_price(self): # private method def get_double_price(self): single_trailing_underscore_ Tkinter.Toplevel(master, class_='ClassName') # Avoid conflict with 'class' keyword list_ = List.objects.get(1) # Avoid conflict with 'list' built-in type __double_leading_underscore class A: def __double_method(self): # for mangling class B(A): Because of the attributes named with double underscore will be mangled like above, we can not access it with “ClassName.__method”. 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 Python Naming. __double_leading_and_trailing_underscore__ class A: def __custom__(self): # custom special method. you might almost do not use it As Internationalization(i18n)/Localization(l10n) functionsIt is just convention, does not have any syntactic functions. That is, the underscore does not means i18n/l10n, and it is just a convention that binds the i18n/l10n to underscore variable has been from C convention. # see official docs : https://docs./3/library/gettext.html gettext.bindtextdomain('myapplication','/path/to/my/language/directory') # ...
To separate the digits of number literal valueThis feature was added in Python 3.6. It is used for separating digits of numbers using underscore for readability. dec_base = 1_000_000 print(dec_base) # 1000000 ConclusionSo far we’ve covered the underscore of Python. While I’m a Python programmer, I didn’t know some of them till wrote this post. Especially, the i18n/l10n is very new to me. Next, I’ll cover more interesting things about Python. Thank you. Update |
|
来自: katwang > 《Knowledge》