本文共 3157 字,大约阅读时间需要 10 分钟。
()
( )
We often find underscore(s) before and after the function name in python while reading the source code. Do you know why and where it has to be used?
在阅读源代码时,我们经常在python中的函数名称前后找到下划线。 您知道为什么要在哪里使用它吗?
I’ll impart the use of underscore in Python’s function name. There are three possibilities of underscore occurred in function name, one underscore at the beginning(_get_name), two underscores at the beginning(__generate), and two underscores at the either side of function(__len__)
我将在Python的函数名称中使用下划线。 函数名称中存在下划线的三种可能性,一个在开头的下划线(_get_name),在开始的两个下划线(__generate),以及在函数的任一侧的两个下划线(__len__)
One underscore at the beginning:
刚开始时,下划线是:
Python doesn’t have real private methods, so one underline in the start of a method or attribute means you shouldn’t access this method because it not the part of API.
Python没有真正的私有方法,因此在方法或属性的开头加下划线表示您不应访问此方法,因为它不是API的一部分。
classBaseForm(StrAndUnicode): def _get_errors(self): "Returns an ErrorDict for the data provided for the form" if self._errors isNone: self.full_clean() return self._errors errors = property(_get_errors)
_get_errors
, is “private”, so you shouldn’t access outside the class.
_get_errors
是“私有”的,因此您不应在类外部访问。
Two underscores in the beginning:
一开始有两个下划线:
It makes a lot of confusion. It should not be used to create a private method. It should be used to avoid your method to be overridden by a subclass.
这引起很多混乱。 不应将其用于创建私有方法。 应该使用它来避免您的方法被子类覆盖。
classclass A A (( objectobject def def __test __test (( selfself print print def def test test (( selfself self self .. __test__test classclass B B (( AA def def __test __test (( selfself print print "I'm test method in class B""I'm test method in class B" a = A() a = A() print a.test() # Output: I'm test method in class A print a.test() # Output: I'm test method in class A b b == B B ()() print bprint b .. testtest () # Output: I'm test method in class A() # Output: I'm test method in class A
As we have seen, b.test()
didn’t call B.__test()
methods, as we could expect. Basically it is the correct behavior for __
. So, when you create a method starting with __
it means that you don’t want anyone to override it, it will be accessible only from inside the class where it was defined.
如我们所见, b.test()
没有 像我们期望的那样 调用 B.__test()
方法。 基本上,这是 __
的正确行为 。 因此,当您创建以 __
开头的方法时, 这意味着您不想让任何人重写它,它只能从定义它的类内部访问。
Two underlines in the beginning and in the end:
开头和结尾有两个下划线:
When we see a method like __this__
, don’t call it. Because it means it’s a method which Python calls, not by you. Let’s take a look:
当我们看到类似 __this__
的方法时 ,不要调用它。 因为这意味着它是Python调用的方法,而不是您调用的方法。 让我们来看看:
There is always an operator or native function which calls these magic methods. Sometimes it’s just a hook Python calls in specific situations. For example __init__() is called when the object is created. __new__() is called to build the instance. For more details will help more.
总是有一个调用这些魔术方法的运算符或本机函数。 有时,在特定情况下,这只是Python调用的一个钩子。 例如,在创建对象时调用__init __()。 调用__new __()构建实例。 有关更多详细信息, 将提供更多帮助。
翻译自:
转载地址:http://hwhwd.baihongyu.com/