python中的下划线

Difference between _, __ and __xx__ in Python

许多人在学习python的时候并没有真正理解下划线的意义,下面给出详细的解释。

One underline in the beginning

Python doesn‘t have real private methods, so one underline in the beginning of a method or attribute means you shouldn‘t access this method, because it‘s not part of the API. It‘s very common when using properties:

Python没有像C++那样的私有域,使用一个下划线开头的方法或属性表示你无法通过对象名或类名访问该方法或属性,即该方法或属性是private的,对外部不可见。

对于properties同样适用。

class BaseForm(StrAndUnicode):
    ...

    def _get_errors(self):
        "Returns an ErrorDict for the data provided for the form"
        if self._errors is None:
            self.full_clean()
        return self._errors

    errors = property(_get_errors)

This snippet was taken from django source code (django/forms/forms.py). This means errors is a property, and it‘s part of the API, but the method this property calls, _get_errors, is "private", so you shouldn‘t access it.

Two underlines in the beginning

This one causes a lot of confusion. It should not be used to mark a method as private, the goal here is to avoid your method to be overridden by a subclass. Let‘s see an example:

双下划线开头的方法是为了防止子类重写该方法。

class A(object):
    def __method(self):
        print "I‘m a method in A"

    def method(self):
        self.__method()

a = A()
a.method()

输出如下:

$ python example.py
I‘m a method in A

输出没问题,现在看下子类也有同样的__method的情况。

class B(A):
    def __method(self):
        print "I‘m a method in B"

b = B()
b.method()

猜猜输出是什么?...

$ python example.py
I‘m a method in A

as you can see, A.method() didn‘t call B.__method() as we could expect. Actually this is the correct behavior for __. So when you create a method starting with __ you‘re saying that you don‘t want anybody to override it, it will be accessible just from inside the own class.

How python does it? Simple, it just renames the method. Take a look:

a = A()
a._A__method()  # never use this!! please!
$ python example.py
I‘m a method in A

If you try to access a.__method() it won‘t work either, as I said, __method is just accessible inside the class itself.

Two underlines in the beginning and in the end

When you see a method like __this__, the rule is simple: don‘t call it. Why? Because it means it‘s a method python calls, not you. Take a look:

>>> name = "igor"
>>> name.__len__()
4
>>> len(name)
4

>>> number = 10
>>> number.__add__(20)
30
>>> number + 20
30

There is always an operator or native function that calls these magic methods. The idea here is to give you the ability to override operators in your own classes. Sometimes it‘s just a hook python calls in specific situations. __init__(), for example, is called when the object is created so you can initialize it. __new__() is called to build the instance, and so on...

Here‘s an example:

class CrazyNumber(object):

    def __init__(self, n):
        self.n = n

    def __add__(self, other):
        return self.n - other

    def __sub__(self, other):
        return self.n + other

    def __str__(self):
        return str(self.n)
num = CrazyNumber(10)
print num           # 10
print num + 5       # 5
print num - 20      # 30

Another example:

class Room(object):

    def __init__(self):
        self.people = []

    def add(self, person):
        self.people.append(person)

    def __len__(self):
        return len(self.people)

room = Room()
room.add("Igor")
print len(room)     # 1

The documentation covers all these special methods.

Conclusion

Use _one_underline to mark you methods as not part of the API. Use__two_underlines__ when you‘re creating objects to look like native python objects or you wan‘t to customize behavior in specific situations. And don‘t use__just_to_underlines, unless you really know what you‘re doing!

时间: 2024-07-30 16:22:37

python中的下划线的相关文章

详解 Python 中的下划线命名规则

在 python 中,下划线命名规则往往令初学者相当 疑惑:单下划线.双下划线.双下划线还分前后……那它们的作用与使用场景 到底有何区别呢?今天 就来聊聊这个话题. 1.单下划线(_) 通常情况下,单下划线(_)会在以下3种场景中使用: 1.1 在解释器中: 在这种情况下,“_”代表交互式解释器会话中上一条执行的语句的结果.这种用法首先被标准CPython解释器采用,然后其他类型的解释器也先后采用. >>> _ Traceback (most recent call last): Fil

Python中的下划线(转)

译文:Python中的下划线 英文原文:Underscores in Python 这篇文章讨论Python中下划线_的使用.跟Python中很多用法类似,下划线_的不同用法绝大部分(不全是)都是一种惯例约定. 1. 单个下划线直接做变量名(_) 主要有三种情况: 1. 解释器中 _符号是指交互解释器中最后一次执行语句的返回结果.这种用法最初出现在CPython解释器中,其他解释器后来也都跟进了. >>> _ Traceback (most recent call last): File

python中的下划线及双下划线

一.Python 用下划线作为变量前缀和后缀指定特殊变量 1. 单下划线开头: _xxx:弱“内部使用”标识,如:”from Module import *”,将不导入所有以下划线开头的对象,包括包.模块.成员 2. 双下划线开头: __xxx:模块内的私有成员,外部无法直接调用. 即:私有类型的变量.只能是允许这个类本身进行访问了.连子类也不可以 3. 双下划线开头和结尾: __xxx__ :系统定义名字, 用户无法控制的命名空间中的“魔术”对象或属性, 如: __name__.__doc__

Python中的下划线(译文)

原文地址这篇文章讨论Python中下划线_的使用.跟Python中很多用法类似,下划线_的不同用法绝大部分(不全是)都是一种惯例约定. 单个下划线(_) 主要有三种情况: 1. 解释器中 _符号是指交互解释器中最后一次执行语句的返回结果.这种用法最初出现在CPython解释器中,其他解释器后来也都跟进了. >>> _ Traceback (most recent call last): File "", line 1, in NameError: name '_' i

【转】关于python中带下划线的变量和函数 的意义

http://www.blogjava.net/lincode/archive/2011/02/02/343859.html 总结: 变量: 1.  前带_的变量:  标明是一个私有变量, 只用于标明, 外部类还是可以访问到这个变量 2.  前带两个_ ,后带两个_ 的变量:  标明是内置变量, 3.  大写加下划线的变量:  标明是 不会发生改变的全局变量 函数: 1. 前带_的变量: 标明是一个私有函数, 只用于标明, 2.  前带两个_ ,后带两个_ 的函数:  标明是特殊函数 Pytho

python中的下划线用法

Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用'from module import *'导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 核心风格:避免用下划线作为变量名的开始. 因为下划线对解释器有特殊的意义,而且是内建标识符所使用的符号,我们建议程序员避免用下划线作为变量名的开始.一般来讲,变量名_xxx被看作是"私有 的",在模块或类外不可以使用.当变量是私有的时候,用_xxx 来表示变量是很好的习惯.因为变量名__xxx__对Pytho

python中带有下划线的变量和函数

一.变量 常量:大写加下划线 USER_CONSTANT 对于不会发生改变的全局变量,使用大写加下划线. 私有变量:小写和一个前导下划线 _private_value python中不存在私有变量一说,若是遇到需要保护的变量,使用小写和一个前导下划线.但这只是程序员之间的一个约定,用于警告说明这是一个私有变量,外部类不要去访问它.但实际上,外部类还是可以访问到这个变量. 内置变量:小写,两个前导下划线和两个后置下划线 __class__ 两个前导下划线会导致变量在解释期间被更名.这是为了避免内置

python中带下划线的变量和函数的意义

表示私有属性,只能在自己的实例方法里面访问. self.__name会被编译成self._Bar__name以达到“不被外部访问”的效果 示例如下: 变量: 1.  前带_的变量:  标明是一个私有变量, 只用于标明, 外部类还是可以访问到这个变量 2.  前带两个_ ,后带两个_ 的变量:  标明是内置变量, 3.  大写加下划线的变量:  标明是 不会发生改变的全局变量 函数: 1. 前带_的变量: 标明是一个私有函数, 只用于标明, 2.  前带两个_ ,后带两个_ 的函数:  标明是特殊

为什么Java7开始在数字中使用下划线

JDK1.7的发布已经介绍了一些有用的特征,尽管大部分都是一些语法糖,但仍然极大地提高了代码的可读性和质量.其中的一个特征是介绍字面常量数字的下划线.从Java7开始,你就可以在你的Java代码里把长整型数字比如10000000000写成一个更具可读性10_000_000_000.在字面常量数字中加下划线的一个重要的原因是避免一些难以通过看代码来发现的细微的错误.对比10000000000 和1000000000,我们很难发现少了一个0或多了一个0,但对于10_000_000_000和1_000