当前位置:   article > 正文

python 函数、变量中单下划线和双下划线的区别_单下划线变量和双下划线区别

单下划线变量和双下划线区别

一、_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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

import lerarn_under_line

>>> import lerarn_under_line
>>> lerarn_under_line.call_var
<function call_var at 0x10aa61850>
>>> 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 "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__call_var'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

from lerarn_under_line import *

>>> from lerarn_under_line import *
>>> course
'math'
>>> _credit
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_credit' is not defined
>>> call_var()
course:math
_credit:4
>>> _call_var()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_call_var' is not defined
>>> __call_var()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__call_var' is not defined
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

import lerarn_under_line

>>> import lerarn_under_line
>>> a=Course('math')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Course' is not defined
  • 1
  • 2
  • 3
  • 4
  • 5

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 "<stdin>", line 1, in <module>
AttributeError: 'Course' object has no attribute '__credit'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

from lerarn_under_line import Course

>>> from lerarn_under_line import Course
>>> a=Course('math')
>>> a.__credit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Course' object has no attribute '__credit'
>>> a._credit()
math的credit 为4
>>> a.credit()
math的credit 为4
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

综上,单下划线开头的函数表示是口头实例私有变量,是可访问的,但是也不要随意访问,即所谓防君子不防小人

二、__func 双下划线开头的函数 --私有变量

2.1、在模块中使用双下划线开头

  • 在实例中,带双下划线的类变量、实例变量、方法不能被直接访问。但有办法间接访问。如1.1中的from module import __func
>>> from lerarn_under_line import *
>>> __call_var()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__call_var' is not defined

>>> import lerarn_under_line
>>> lerarn_under_line.__call_var()   # 双下划线不可调用
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2.2、在类中使用双下划线开头

  • 在class类的内部,带双下划线的类变量、实例变量、方法具有正常访问权限。
  • 在继承结构中,带双下划线的基类的类变量和实例变量不能被子类直接访问。

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   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
>>> import lerarn_under_line
>>> a = Course('math', 'zhangyu', 'juyiting')  # 无法实例化
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
>>> import lerarn_under_line
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lerarn_under_line.py", line 77, in <module>
    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 "<stdin>", line 1, in <module>
  File "lerarn_under_line.py", line 77, in <module>
    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 "<stdin>", line 1, in <module>
  File "lerarn_under_line.py", line 77, in <module>
    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 "<stdin>", line 1, in <module>
  File "lerarn_under_line.py", line 77, in <module>
    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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

三、前后都有双下划线 --特殊变量

Python保留了有双前导和双末尾下划线的名称,用于特殊用途。 这样的例子有,init__对象构造函数,或__call — 它使得一个对象可以被调用。这些方法通常被称为神奇方法,最好避免在自己的程序中使用以双下划线开头和结尾的名称,以避免与将来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#sc=2340

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/601111
推荐阅读
相关标签
  

闽ICP备14008679号