目录
- 一、_func 单下划线开头 --口头私有变量
- 1.1、在模块中使用单下划线开头
- 1.2、在类中使用单下划线开头
- 二、__func 双下划线开头的函数 --私有变量
- 2.1、在模块中使用双下划线开头
- 2.2、在类中使用双下划线开头
- 三、前后都有双下划线 --特殊变量
一、_func 单下划线开头 --口头私有变量
1.1、在模块中使用单下划线开头
在 Python 中,利用单下划线 `_` 可实现模块级别的私有化效果,但变量除外。通常约定以单下划线开头的函数属于模块私有,这意味着执行 `from moduleName import *` 时,不会导入这些以单下划线命名的函数。该规则同样适用于模块中定义的函数、全局变量和类。尽管默认不导入,仍可通过显式指定 `from module import _func` 的方式单独导入。
具体实例如下所示:
lerarn_under_line.py
# coding=utf-8
course = "math"
_credit = 4
def call_var():
print "course:%s" % course
print "_credit:%d" % _credit
def _call_var():
print "带下划线course:%s" % course
print "带下划线_credit:%d" % _credit
def __call_var():
print "带双下划线course:%s" % course
print "带双下划线_credit:%d" % _credit
import lerarn_under_line
>>> import lerarn_under_line
>>> lerarn_under_line.call_var
>>> lerarn_under_line.call_var()
course:math
_credit:4
>>> lerarn_under_line._call_var() # 单下划线可以调用
带下划线course:math
带下划线_credit:4
>>> >>> lerarn_under_line.__call_var() # 双下划线不可调用
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute '__call_var'
from lerarn_under_line import *
>>> from lerarn_under_line import *
>>> course
'math'
>>> _credit
Traceback (most recent call last):
File "", line 1, in
NameError: name '_credit' is not defined
>>> call_var()
course:math
_credit:4
>>> _call_var()
Traceback (most recent call last):
File "", line 1, in
NameError: name '_call_var' is not defined
>>> __call_var()
Traceback (most recent call last):
File "", line 1, in
NameError: name '__call_var' is not defined
from module import _func
>>> from lerarn_under_line import course
>>> course
'math'
>>> from lerarn_under_line import _credit
>>> _credit
4
>>> from lerarn_under_line import call_var
>>> call_var()
course:math
_credit:4
>>> from lerarn_under_line import _call_var
>>> _call_var()
带下划线course:math
带下划线_credit:4
>>> from lerarn_under_line import __call_var
>>> __call_var()
带双下划线course:math
带双下划线_credit:4
1.2、在类中使用单下划线开头
lerarn_under_line.py
class Course(object):
def __init__(self, name):
self.name = name
def credit(self):
if self.name == 'math':
print "%s的credit 为%d" % (self.name, 4)
else:
print "%s的credit 为%d" % (self.name, 2)
def _credit(self):
if self.name == 'math':
print "%s的credit 为%d" % (self.name, 4)
else:
print "%s的credit 为%d" % (self.name, 2)
def __credit(self):
if self.name == 'math':
print "%s的credit 为%d" % (self.name, 4)
else:
print "%s的credit 为%d" % (self.name, 2)
import lerarn_under_line
>>> import lerarn_under_line
>>> a=Course('math')
Traceback (most recent call last):
File "", line 1, in
NameError: name 'Course' is not defined
from lerarn_under_line import *
>>> from lerarn_under_line import *
>>> a=Course('math')
>>> a.credit()
math的credit 为4
>>> a._credit()
math的credit 为4
>>> a.__credit()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'Course' object has no attribute '__credit'
from lerarn_under_line import Course
>>> from lerarn_under_line import Course
>>> a=Course('math')
>>> a.__credit()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'Course' object has no attribute '__credit'
>>> a._credit()
math的credit 为4
>>> a.credit()
math的credit 为4
综上所述,以单下划线开头的函数被视为一种“口头”私有变量。它们在技术上是可以被访问的,但基于编程规范,不应随意调用。这种设计旨在提醒开发者这是内部实现细节,即所谓“防君子不防小人”。
二、__func 双下划线开头的函数 --私有变量
2.1、在模块中使用双下划线开头
在实例中,带双下划线的类变量、实例变量、方法不能被直接访问。但有办法间接访问。如1.1中的from module import __func
>>> from lerarn_under_line import *
>>> __call_var()
Traceback (most recent call last):
File "", line 1, in
NameError: name '__call_var' is not defined
>>> import lerarn_under_line
>>> lerarn_under_line.__call_var() # 双下划线不可调用
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute '__call_var'
>>> from lerarn_under_line import course
>>> from lerarn_under_line import __call_var
>>> __call_var()
带双下划线course:math
带双下划线_credit:4
2.2、在类中使用双下划线开头
- 在类的内部定义中,以双下划线开头的类变量、实例变量和方法拥有正常的访问权限,可以在类的方法中被调用。
- 然而,在继承结构中,以双下划线开头的基类成员变量和实例变量会被 Python 解释器进行名称修饰(Name Mangling),导致子类无法直接通过原名称访问这些属性,从而实现了真正的私有化保护。
lerarn_under_line.py
class Course(object):
def __init__(self, name, _teacher, __classroom):
self.name = name
self._teacher = _teacher
self.__classroom = __classroom
def call_var(self):
print "name:%s" % self.name
print "_teacher:%s" % self._teacher
print "__classroom:%s" % self.__classroom
>>> import lerarn_under_line
>>> a = Course('math', 'zhangyu', 'juyiting') # 无法实例化
Traceback (most recent call last):
File "", line 1, in
NameError: name 'Course' is not defined
>>> from lerarn_under_line import *
>>> a = Course('math', 'zhangyu', 'juyiting')
>>> a.call_var()
name:math
_teacher:zhangyu
__classroom:juyiting
lerarn_under_line.py
class Course(object):
def __init__(self, name, _teacher, __classroom):
self.name = name
self._teacher = _teacher
self.__classroom = __classroom
def call_var(self):
print "name:%s" % self.name
print "_teacher:%s" % self._teacher
print "__classroom:%s" % self.__classroom
class SonCourse(Course):
def __init__(self, name, _teacher, __classroom, time):
super(Course, self).__init__()
self.time = time
self.name = name
self.__classroom = self.__classroom
self._teacher = self._teacher
self.__classroom = self.__classroom
def call_son_var(self):
print "time:%s" % self.time
print "name:%s" % self.name
print "_teacher:%s" % self._teacher
print "__classroom:%s" % self.__classroom
>>> import lerarn_under_line
Traceback (most recent call last):
File "", line 1, in
File "lerarn_under_line.py", line 77, in
b = SonCourse('math', 'zhangyu', 'juyiting', "12:00")
File "lerarn_under_line.py", line 63, in __init__
self.__classroom = self.__classroom
AttributeError: 'SonCourse' object has no attribute '_SonCourse__classroom'
>>> from lerarn_under_line import *
Traceback (most recent call last):
File "", line 1, in
File "lerarn_under_line.py", line 77, in
b = SonCourse('math', 'zhangyu', 'juyiting', "12:00")
File "lerarn_under_line.py", line 63, in __init__
self.__classroom = self.__classroom
AttributeError: 'SonCourse' object has no attribute '_SonCourse__classroom'
>>> from lerarn_under_line import Course
Traceback (most recent call last):
File "", line 1, in
File "lerarn_under_line.py", line 77, in
b = SonCourse('math', 'zhangyu', 'juyiting', "12:00")
File "lerarn_under_line.py", line 63, in __init__
self.__classroom = self.__classroom
AttributeError: 'SonCourse' object has no attribute '_SonCourse__classroom'
>>> from lerarn_under_line import sonCourse
Traceback (most recent call last):
File "", line 1, in
File "lerarn_under_line.py", line 77, in
b = SonCourse('math', 'zhangyu', 'juyiting', "12:00")
File "lerarn_under_line.py", line 63, in __init__
self.__classroom = self.__classroom
AttributeError: 'SonCourse' object has no attribute '_SonCourse__classroom'
三、前后都有双下划线 --特殊变量
Python 语言保留了以双下划线开头和结尾的名称,专门用于特殊用途。例如 `__init__` 是对象构造函数,`__call__` 使得对象可被调用。这类方法通常被称为“魔术方法”(Magic Methods)。为了避免与未来 Python 语言版本的变化产生冲突,建议开发者不要在自己的程序中定义以双下划线开头和结尾的名称。
常见方法包括:
| 方法 | 含义 |
|---|---|
| __str__ | 当将对象转换成字符串时会执行 |
| __init__ | 初始化方法,为对象变量赋值 |
| __new__ | 构造方法,创建一个对象 |
| __call__ | 在对象后面加括号会执行该方法 |
| __getattr__ | 当使用对象.属性时,若属性不存在会调用该方法 |
| __setattr__ | 当使用对象.属性 = 值,会调用该方法 |
| __iter__ | 类内部定义了该方法,对象就变成了可迭代对象 |
| __add__ | 当两个对象使用+号会调用该方法 |
| __enter__和__exit__ | 上下文管理 |
参考文档
1、https://blog.csdn.net/brucewong0516/article/details/79120841
2、http://t.zoukankan.com/one-tom-p-11749739.html
3、https://www.cnblogs.com/bryant24/p/11429653.html
4、https://blog.csdn.net/m0_58357932/article/details/121062461
5、https://www.likecs.com/show-308380836.html
到此这篇关于 Python 函数、变量中单下划线和双下划线的区别详解的文章就介绍到这了,更多相关 Python 单下划线和双下划线区别内容请搜索本站以前的文章或继续浏览下面的相关文章,希望大家以后多多支持本站!