Python中定义“私有”成员变量和成员函数
2016-10-08 11:47
519 查看
出处:/Uploads/Images/Content/201409/e983d8adf98e585a0244a260b1e61a69
在学习Python的过程中发下,它把类(class)中所有的成员函数和成员变量都看做是"Public"的,作为C++出身的程序员们可能就不习惯了。
Python的官方教程中如是说:““Private” instance variables that cannot be accessed except from inside an object don’t exist in Python.”。也就是说,在Python中我们不能够像C++或者Java那样有专门的private和public关键字来指定某些成员是公有的,哪些成员是私有的。
然而,Python教程中又说了:“However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam)
should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.”。
OK,原来在Python中大家都是通过在一个变量或者函数之前加上下划线来表示私有变量的,例如__spam(这里是两个下划线)就是私有的。
同时,Python为了保证不能再class之外访问该变量,“Any identifier of the form __spam (at
least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam,
where classname is
the current class name with leading underscore(s) stripped.”,意思就是说,Python会在类的内部自动的把你定义的__spam变量的名字替换成为 _classname__spam(注意,classname前面是一个下划线,spam前是两个下划线),Python把这种技术叫做“name mangling”。因此,用户在外部访问__spam的时候就会提示找不到相应的变量。
下面给出一段简单的代码:
[python]
view plain
copy
#!/usr/bin/python
#Filename: classPrivate.py
class testPrivate:
def __init__(self):
self.__data = []
def add(self,item):
self.__data.append(item)
def printData(self):
print (self.__data)
t = testPrivate()
t.add('dancingrain')
t.add('hello')
t.printData()
print(t.__data)
输出结果是:
可以看出,当执行print(t.__data)的时候提示我们没有该属性,OK!这就是我们想要的结果。
但是,这并不是意味着我们真的就不能够从外部访问这个变量了,上面说Python在类的内部用_classname__spam替换了__spam,
因此,我们可以在类的外面使用_classname__spam来访问__spam。看看下面的代码:
[python]
view plain
copy
#!/usr/bin/python
#Filename: classPrivate.py
class testPrivate:
def __init__(self):
self.__data = []
def add(self,item):
self.__data.append(item)
def printData(self):
print (self.__data)
t = testPrivate()
t.add('dancingrain')
t.add('hello')
t.printData()
print(t._testPrivate__data)
结果是:
OK,我们在外面也能够访问该“私有”变量了,
这一点在调试中是比较有用的!
在学习Python的过程中发下,它把类(class)中所有的成员函数和成员变量都看做是"Public"的,作为C++出身的程序员们可能就不习惯了。
Python的官方教程中如是说:““Private” instance variables that cannot be accessed except from inside an object don’t exist in Python.”。也就是说,在Python中我们不能够像C++或者Java那样有专门的private和public关键字来指定某些成员是公有的,哪些成员是私有的。
然而,Python教程中又说了:“However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam)
should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.”。
OK,原来在Python中大家都是通过在一个变量或者函数之前加上下划线来表示私有变量的,例如__spam(这里是两个下划线)就是私有的。
同时,Python为了保证不能再class之外访问该变量,“Any identifier of the form __spam (at
least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam,
where classname is
the current class name with leading underscore(s) stripped.”,意思就是说,Python会在类的内部自动的把你定义的__spam变量的名字替换成为 _classname__spam(注意,classname前面是一个下划线,spam前是两个下划线),Python把这种技术叫做“name mangling”。因此,用户在外部访问__spam的时候就会提示找不到相应的变量。
下面给出一段简单的代码:
[python]
view plain
copy
#!/usr/bin/python
#Filename: classPrivate.py
class testPrivate:
def __init__(self):
self.__data = []
def add(self,item):
self.__data.append(item)
def printData(self):
print (self.__data)
t = testPrivate()
t.add('dancingrain')
t.add('hello')
t.printData()
print(t.__data)
输出结果是:
可以看出,当执行print(t.__data)的时候提示我们没有该属性,OK!这就是我们想要的结果。
但是,这并不是意味着我们真的就不能够从外部访问这个变量了,上面说Python在类的内部用_classname__spam替换了__spam,
因此,我们可以在类的外面使用_classname__spam来访问__spam。看看下面的代码:
[python]
view plain
copy
#!/usr/bin/python
#Filename: classPrivate.py
class testPrivate:
def __init__(self):
self.__data = []
def add(self,item):
self.__data.append(item)
def printData(self):
print (self.__data)
t = testPrivate()
t.add('dancingrain')
t.add('hello')
t.printData()
print(t._testPrivate__data)
结果是:
OK,我们在外面也能够访问该“私有”变量了,
这一点在调试中是比较有用的!
相关文章推荐
- Python中定义“私有”成员变量和成员函数
- Python中定义“私有”成员变量和成员函数
- Python中定义“私有”成员变量和成员函数
- Python中定义“私有”成员变量和成员函数
- Python中定义“私有”成员变量和成员函数
- Python中定义“私有”成员变量和成员函数
- Python中定义“私有”成员变量和成员函数
- 定义一个包含私有成员变量和函数的类,再定义一个内部类,在内部类函数中访问外部成员变量,并调用外部函数。在外部类函数中创建内部类对象,调用内部类函数
- 导出DLL 隐藏私有成员变量和成员函数 工厂方法
- 如何定义一个类成员的指针(成员变量或成员函数)
- python中类的定义、实例化、封装以及私有变量/方法
- python 中的私有变量申明和定义
- C++定义一个类,如果成员变量没有说明是共有还是私有那默认的是哪一个?
- 使用Python类的 __init__( ) 函数定义成员变量
- Java:【面向对象:类的定义,静态变量,成员变量,构造函数,封装与私有,this】
- C++定义一个类,如果成员变量没有说明是共有还是私有那默认的是哪一个?
- 在Objective C中定义私有的成员变量
- python中的类变量、成员变量和成员函数的局部变量使用
- 类成员变量和成员函数的地址
- 请不要将成员变量定义成public