博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中函数名称前后的下划线用法
阅读量:2519 次
发布时间:2019-05-11

本文共 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 __()构建实例。 有关更多详细信息, 将提供更多帮助。

Current rating: 4
当前评分:4

翻译自:

转载地址:http://hwhwd.baihongyu.com/

你可能感兴趣的文章
C#身份证号码校验
查看>>
JS函数调用方式
查看>>
HDU2519:新生晚会
查看>>
第二周作业
查看>>
2019学期第四周编程总结 .
查看>>
进程和线程区别
查看>>
微信小程序小技巧系列《二》show内容展示,上传文件编码问题
查看>>
动态样式语言Sass&Less介绍与区别
查看>>
开心菜鸟系列----函数作用域(javascript入门篇)
查看>>
详解 UIView 的 Tint Color 属性
查看>>
仿真和计算作业
查看>>
微软面试题答案
查看>>
WebService - 创建
查看>>
第一章《人造与天生》
查看>>
centos7 install rabbtimq
查看>>
hdu 1002 A+B Problem 2
查看>>
消息队列五
查看>>
Ubuntu 14.04 64bit下Caffe + Cuda6.5/Cuda7.0 安装配置教程
查看>>
js中期知识点总结11月2日
查看>>
20150716 DAY5
查看>>