python函数、变量中单下划线和双下划线的区别-创新互联

一、_func单下划线开头 --口头私有变量 1.1、在模块中使用单下划线开头

在Python中,通过单下划线_来实现模块级别的私有化,变量除外。一般约定以单下划线开头的函数为模块私有的,也就是说from moduleName import *将不会引入以单下划线开头的函数。模块中使用单下划线开头定义函数、全局变量和类均适用,但可以用:from module import _func形式单独导入。
实例如下:
lerarn_under_line.py

蕉城ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
# 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, inAttributeError: '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, inNameError: name '_credit' is not defined
>>>call_var()
course:math
_credit:4
>>>_call_var()
Traceback (most recent call last):
  File "", line 1, inNameError: name '_call_var' is not defined
>>>__call_var()
Traceback (most recent call last):
  File "", line 1, inNameError: 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, inNameError: 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, inAttributeError: '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, inAttributeError: '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, inNameError: name '__call_var' is not defined

>>>import lerarn_under_line
>>>lerarn_under_line.__call_var()   # 双下划线不可调用
Traceback (most recent call last):
  File "", line 1, inAttributeError: '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、在类中使用双下划线开头
  • 在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  
>>>import lerarn_under_line
>>>a = Course('math', 'zhangyu', 'juyiting')  # 无法实例化
Traceback (most recent call last):
  File "", line 1, inNameError: 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, inFile "lerarn_under_line.py", line 77, inb = 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, inFile "lerarn_under_line.py", line 77, inb = 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, inFile "lerarn_under_line.py", line 77, inb = 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, inFile "lerarn_under_line.py", line 77, inb = 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 — 它使得一个对象可以被调用。这些方法通常被称为神奇方法,最好避免在自己的程序中使用以双下划线开头和结尾的名称,以避免与将来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

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻标题:python函数、变量中单下划线和双下划线的区别-创新互联
新闻来源:http://myzitong.com/article/ccpjsp.html