Python中的下划线( |
您所在的位置:网站首页 › 在python中!的用法 › Python中的下划线( |
前言
在Python中,下划线(_)是特殊的。如果您是python程序员,对于for _ in range(10),以及__init__(self)的语法可能比较熟悉。 这篇文章将解释什么时候以及如何使用下划线(_),并帮助你理解它。 在Python中有5种使用下划线的情况: 1、用于在解释器中存储最后一个表达式的值。 2、忽略特定的值。 3、给变量名或函数名赋予特殊的意义和功能。 4、用作“国际化(i18n)”或“本地化(l10n)”。 5、将数字的值分开。 在解释器中使用时python解释器将最后一个表达式值存储到名为’ _ '的特殊变量中。这个特性首先在标准的CPython解释器中使用,您也可以在其他Python解释器中使用它。 >>> 10 10 >>> _ 10 >>> _ * 3 30 >>> _ * 20 600 忽略特定的值下划线还用于忽略特定的值。如果不需要特定的值,或者不使用这些值,只需将这些值赋给下划线即可。 # Ignore a value when unpacking x, _, y = (1, 2, 3) # x = 1, y = 3 # Ignore the multiple values. It is called "Extended Unpacking" which is available in only Python 3.x x, *_, y = (1, 2, 3, 4, 5) # x = 1, y = 5 # Ignore the index for _ in range(10): do_something() # Ignore a value of specific location for _, val in list_of_tuple: do_something() 赋予变量和函数名称特殊的含义下划线可能最常用于“命名”。PEP8是Python的约定准则,它介绍了以下4种命名情况: _single_leading_underscore (首部单下划线)此约定用于声明模块中的私有变量、函数、方法和类。在 from module import *中,任何具有此约定的内容都将被忽略。 然而,当然,Python不支持真正的私有,所以我们不能强制一些私有的东西,也可以直接从其他模块调用它。所以有时我们会说“弱内部使用指标”。 _internal_name = 'one_nodule' # private variable _internal_version = '1.0' # private variable class _Base: # private class _hidden_factor = 2 # private variable def __init__(self, price): self._price = price def _double_price(self): # private method return self._price * self._hidden_factor def get_double_price(self): return self._double_price() single_trailing_underscore_ (尾部单下滑线)此约定可用于避免与Python关键字或内置项发生冲突。你可能不经常使用它。 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 (首部双下划线)这是语法而不是约定的。双下划线将”矫正“类的属性名,以避免类之间的属性名冲突。(所谓的“矫正”是指编译器或解释器用一些规则修改变量或函数名,而不是按原样使用) Python的矫正规则是在属性名前面加上双下划线声明“_ClassName”。也就是说,如果你在一个类中编写了一个名为“__method”的方法,那么这个名字将会在“_ClassName__method”的表单中被矫正。 class A: def _single_method(self): pass def __double_method(self): # for mangling pass class B(A): def __double_method(self): # for mangling pass因为用双下划线命名的属性会像上面那样矫正,所以我们不能用“ClassName.__method”访问它。有时,有些人使用它就像真正的私人使用这些功能,但它不是私人的,也不推荐这样做。 double_leading_and_trailing_underscore (首尾部双下划线)这个约定用于特殊的变量或方法(所谓的“魔法方法”),如:init, len。这些方法提供了特殊的语法特征或做了特殊的事情。例如,__file__表示Python文件的位置,当a==b表达式被执行时,__eq__被执行。 class A: def __init__(self, a): # use special method '__init__' for initializing self.a = a def __custom__(self): # custom special method. you might almost do not use it pass 国际化(i18n) /本地化(l10n)功能它只是约定,没有任何语法功能。也就是说,下划线并不意味着i18n/l10n,它只是将i18n/l10n绑定到下划线变量,这个约定来自C语言约定。用于i18n/l10n的内置库gettext使用了这种约定,Python web框架Django支持i18n/l10n也引入并使用了这种约定。 # see official docs : https://docs.python.org/3/library/gettext.html import gettext gettext.bindtextdomain('myapplication','/path/to/my/language/directory') gettext.textdomain('myapplication') _ = gettext.gettext # ... print(_('This is a translatable string.')) 将数字值分开这个特性是在Python 3.6中添加的。它用于使用下划线来分隔数字,以提高可读性 dec_base = 1_000_000 bin_base = 0b_1111_0000 hex_base = 0x_1234_abcd print(dec_base) # 1000000 print(bin_base) # 240 print(hex_base) # 305441741参考链接 Python中的下划线(_)的五种用法 :https://mp.weixin.qq.com/s/WatFJ_7ujOnsn56I6jhPgA |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |