最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python?函數(shù)、變量中單下劃線和雙下劃線的區(qū)別詳解

 更新時(shí)間:2023年01月18日 16:13:10   作者:岳來(lái)  
本文主要介紹了python?函數(shù)、變量中單下劃線和雙下劃線的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、_func 單下劃線開(kāi)頭 --口頭私有變量

1.1、在模塊中使用單下劃線開(kāi)頭

在Python中,通過(guò)單下劃線_來(lái)實(shí)現(xiàn)模塊級(jí)別的私有化,變量除外。一般約定以單下劃線開(kāi)頭的函數(shù)為模塊私有的,也就是說(shuō)from moduleName import * 將不會(huì)引入以單下劃線開(kāi)頭的函數(shù)。模塊中使用單下劃線開(kāi)頭定義函數(shù)、全局變量和類均適用,但可以用:from module import _func形式單獨(dú)導(dǎo)入。

實(shí)例如下:

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
<function call_var at 0x10aa61850>
>>> lerarn_under_line.call_var()
course:math
_credit:4
>>> lerarn_under_line._call_var() ? # 單下劃線可以調(diào)用
帶下劃線course:math
帶下劃線_credit:4
>>> >>> lerarn_under_line.__call_var() ? # 雙下劃線不可調(diào)用
Traceback (most recent call last):
? File "<stdin>", line 1, in <module>
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 "<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

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、在類中使用單下劃線開(kāi)頭

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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
AttributeError: 'Course' object has no attribute '__credit'
>>> a._credit()
math的credit 為4
>>> a.credit()
math的credit 為4

綜上,單下劃線開(kāi)頭的函數(shù)表示是口頭實(shí)例私有變量,是可訪問(wèn)的,但是也不要隨意訪問(wèn),即所謂防君子不防小人。

二、__func 雙下劃線開(kāi)頭的函數(shù) --私有變量

2.1、在模塊中使用雙下劃線開(kāi)頭

在實(shí)例中,帶雙下劃線的類變量、實(shí)例變量、方法不能被直接訪問(wèn)。但有辦法間接訪問(wèn)。如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() ? # 雙下劃線不可調(diào)用
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

2.2、在類中使用雙下劃線開(kāi)頭

  • 在class類的內(nèi)部,帶雙下劃線的類變量、實(shí)例變量、方法具有正常訪問(wèn)權(quán)限。
  • 在繼承結(jié)構(gòu)中,帶雙下劃線的基類的類變量和實(shí)例變量不能被子類直接訪問(wèn)。

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') ?# 無(wú)法實(shí)例化
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

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 "<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'

三、前后都有雙下劃線 --特殊變量

Python保留了有雙前導(dǎo)和雙末尾下劃線的名稱,用于特殊用途。 這樣的例子有,init__對(duì)象構(gòu)造函數(shù),或__call — 它使得一個(gè)對(duì)象可以被調(diào)用。這些方法通常被稱為神奇方法,最好避免在自己的程序中使用以雙下劃線開(kāi)頭和結(jié)尾的名稱,以避免與將來(lái)Python語(yǔ)言的變化產(chǎn)生沖突。

常見(jiàn)方法:

方法含義
__str__當(dāng)將對(duì)象轉(zhuǎn)換成字符串時(shí)會(huì)執(zhí)行
__init__初始化方法,為對(duì)象變量賦值
__new__構(gòu)造方法,創(chuàng)建一個(gè)對(duì)象
__call__在對(duì)象后面加括號(hào)會(huì)執(zhí)行該方法
__getattr__當(dāng)使用對(duì)象.屬性時(shí),若屬性不存在會(huì)調(diào)用該方法
__setattr__當(dāng)使用對(duì)象.屬性 = 值,會(huì)調(diào)用該方法
__iter__類內(nèi)部定義了該方法,對(duì)象就變成了可迭代對(duì)象
__add__當(dāng)兩個(gè)對(duì)象使用+號(hào)會(huì)調(diào)用該方法
__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

到此這篇關(guān)于python 函數(shù)、變量中單下劃線和雙下劃線的區(qū)別詳解的文章就介紹到這了,更多相關(guān)python  單下劃線和雙下劃線區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python“靜態(tài)”變量、實(shí)例變量與本地變量的聲明示例

    python“靜態(tài)”變量、實(shí)例變量與本地變量的聲明示例

    這篇文章主要給大家介紹了關(guān)于python“靜態(tài)”變量、實(shí)例變量與本地變量的聲明的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 用Python實(shí)現(xiàn)讀寫(xiě)鎖的示例代碼

    用Python實(shí)現(xiàn)讀寫(xiě)鎖的示例代碼

    這篇文章主要介紹了用Python實(shí)現(xiàn)讀寫(xiě)鎖的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • 如何用pandas讀取一個(gè)文件或某個(gè)文件夾下所有文件

    如何用pandas讀取一個(gè)文件或某個(gè)文件夾下所有文件

    這篇文章主要介紹了如何用pandas讀取一個(gè)文件或某個(gè)文件夾下所有文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python運(yùn)維自動(dòng)化psutil模塊的監(jiān)控和管理深入探究

    Python運(yùn)維自動(dòng)化psutil模塊的監(jiān)控和管理深入探究

    這篇文章主要為大家介紹了Python運(yùn)維自動(dòng)化psutil模塊的監(jiān)控和管理深入探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • pycharm多光標(biāo)設(shè)置方式

    pycharm多光標(biāo)設(shè)置方式

    PyCharm中多光標(biāo)設(shè)置方法:Alt+Shift+Insert設(shè)置多光標(biāo),按住左鍵拉動(dòng)范圍行會(huì)出現(xiàn)光標(biāo),Alt+Shift+Ctrl加左鍵點(diǎn)擊任意位置即可添加光標(biāo)
    2026-01-01
  • 一文詳細(xì)介紹numpy在python中的用法

    一文詳細(xì)介紹numpy在python中的用法

    這篇文章主要介紹了numpy在python中的用法,NumPy是Python科學(xué)計(jì)算庫(kù),主要用于處理大型多維數(shù)組和矩陣運(yùn)算,它提供了多種函數(shù)進(jìn)行數(shù)組操作,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-01-01
  • Python將QQ聊天記錄生成詞云的示例代碼

    Python將QQ聊天記錄生成詞云的示例代碼

    這篇文章主要介紹了Python將QQ聊天記錄生成詞云的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式

    python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式

    Python內(nèi)置了CSV模塊,可直接通過(guò)該模塊實(shí)現(xiàn)csv文件的讀寫(xiě)操作,在web應(yīng)用中導(dǎo)出數(shù)據(jù)是比較常見(jiàn)操作,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • python實(shí)現(xiàn)單機(jī)五子棋對(duì)戰(zhàn)游戲

    python實(shí)現(xiàn)單機(jī)五子棋對(duì)戰(zhàn)游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)單機(jī)五子棋對(duì)戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Python全景系列之控制流程盤(pán)點(diǎn)及進(jìn)階技巧

    Python全景系列之控制流程盤(pán)點(diǎn)及進(jìn)階技巧

    這篇文章主要為大家介紹了Python全景系列之控制流程盤(pán)點(diǎn)及進(jìn)階技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05

最新評(píng)論

额尔古纳市| 晋中市| 宜丰县| 祁门县| 吴旗县| 新密市| 达孜县| 新巴尔虎左旗| 晋江市| 普安县| 吴堡县| 怀宁县| 宝应县| 含山县| 桐乡市| 平远县| 高要市| 伊吾县| 南川市| 新乡市| 双城市| 勐海县| 桐梓县| 凯里市| 海林市| 上林县| 高雄县| 博罗县| 新营市| 宁津县| 盐源县| 新营市| 山阴县| 扬州市| 莱芜市| 仁化县| 寻甸| 沈阳市| 永康市| 洪雅县| 丹棱县|