Python下划线和私有变量

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.py
a = 1
_a = 2
__a = 3
##test.py
import m
from m import *
print a # 打印1
print _a # 异常,NameError: name '_a' is not defined
print m.a # 打印1
print m._a # 打印2

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

##c.py
class C(object):
    a = 1
    _a = 2
## test.py
from c import C
o = C()
print o.a  #打印1
print o._a #打印2
o._a = 3
print o._a #打印3

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

单下划线结尾

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

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

双下划线开头

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

##c.py
class C(object):
    a = 1
    _a = 2
    __a = 3

##test.py
from c import C

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

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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值