Python下划线和私有变量

来源:互联网 发布:ps vr 知乎 编辑:程序博客网 时间:2024/06/07 04:33

Python下划线和私有变量

最近小组python氛围浓厚,有同学发现自己在一个类里定义了一个方法,但不能用getattr调用。我整理一下python中下划线在命名时的特殊用法。

规范描述

根据PEP-0008的规范:

In addition, the following special forms using leading or trailing
underscores are recognized (these can generally be combined with any
case convention):

  • _single_leading_underscore: weak “internal use” indicator.
    E.g. from M import * does not import objects whose name starts
    with an underscore.

  • single_trailing_underscore_: used by convention to avoid
    conflicts with Python keyword, e.g. ::

         Tkinter.Toplevel(master, class_='ClassName')
  • __double_leading_underscore: when naming a class attribute,
    invokes name mangling (inside class FooBar, __boo becomes
    _FooBar__boo; see below).

  • __double_leading_and_trailing_underscore__: “magic” objects or attributes that live in user-controlled namespaces.
    E.g. __init__, __import__ or __file__. Never invent
    such names; only use them as documented.

是的,python中下划线在命名时是有规范的。下面分别举例说明。

单下划线开头

单下划线开头的命名,规范里描述为“弱内部引用”,一般对应“protect”。

##m.pya = 1_a = 2__a = 3##test.pyimport mfrom m import *print a # 打印1print _a # 异常,NameError: name '_a' is not definedprint m.a # 打印1print m._a # 打印2

import *时不能导入单下划线开头命名的变量或方法。访问的时候可以通过“模块.变量”。这里的protect并非严格的。

##c.pyclass C(object):    a = 1    _a = 2## test.pyfrom c import Co = C()print o.a  #打印1print o._a #打印2o._a = 3print o._a #打印3

我们可以看到类中_a也是可以通过对象直接访问的。这里的“protect”是一种规范,我们应该按照规范来编写代码。

单下划线结尾

单下划线结尾的命名用来与python中有保留字段避免冲突,pep8给的例子

Tkinter.Toplevel(master, class_='ClassName')

双下划线开头

双下滑线开头被认为是私有的。python中的private并非像C++中那样严格定义,而是通过改编命名,将双下滑线开头的命名前面加上单下划线+类名。

##c.pyclass C(object):    a = 1    _a = 2    __a = 3##test.pyfrom c import Co = C()print o.a #打印1print o._a #打印2print o.__a #异常,AttributeError: 'C' object has no attribute '__a'print o._C__a #打印3

所以,双下划线开头的命名用getattr(“__xxx”)是获取不到的,但是用getattr(“_Class__xxx”)就可以访问。不过这里还是要提一个规范的问题。既然我们用双下划线开头,就是希望作为一个私有的变量或方法。那我们应该用私有的用法去使用。

双下划线开头、双下划线结尾

像__init__,__new__,这种,作为python class的特殊用法专用的标识,就不多举例了。

0 0
原创粉丝点击