第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為什么Python的“私有”方法實(shí)際上不是私有的?

為什么Python的“私有”方法實(shí)際上不是私有的?

慕桂英546537 2019-06-23 15:47:12
為什么Python的“私有”方法實(shí)際上不是私有的?Python使我們能夠通過在名稱前加上雙下劃線來創(chuàng)建類中的“私有”方法和變量,如下所示:__myPrivateMethod()..那么,如何解釋這一點(diǎn)呢?>>> class MyClass:...     def myPublicMethod(self):...             print 'public method'...     def __myPrivateMethod(self):...              print 'this is private!!'... >>> obj = MyClass()>>> obj.myPublicMethod()public method           >>> obj.__myPrivateMethod()Traceback (most recent call last):   File "", line 1, in AttributeError: MyClass instance has no attribute '__myPrivateMethod'>>> dir(obj)['_MyClass__myPrivateMethod', '_   _doc__', '__module__', 'myPublicMethod']>>> obj._MyClass__myPrivateMethod()this is private!!怎么回事?!我給那些不太明白的人解釋一下。>>> class MyClass:...     def myPublicMethod(self):...             print 'public method'...     def __myPrivateMethod(self):...              print 'this is private!!'... >>> obj = MyClass()我所做的就是用一個(gè)公共方法和一個(gè)私有方法創(chuàng)建一個(gè)類并實(shí)例化它。接下來,我稱之為它的公共方法。>>> obj.myPublicMethod()public method接下來,我嘗試調(diào)用它的私有方法。>>> obj.__myPrivateMethod()Traceback (most recent call last):   File "", line 1, in AttributeError: MyClass instance has no attribute '__myPrivateMethod'這里一切都很好,我們不能稱之為。事實(shí)上,它是‘私人的’。其實(shí)不是。跑迪爾()在對象上,揭示了python為您的所有“私有”方法神奇地創(chuàng)建的一種新的神奇方法。>>> dir(obj)['_MyClass__myPrivateMethod', '__doc__', '__module__', 'myPublicMethod']此新方法的名稱始終是下劃線,后面是類名,后面是方法名。>>> obj._MyClass__myPrivateMethod()this is private!!封裝就這么多了,嗯?在任何情況下,我都聽說Python不支持封裝,那么為什么還要嘗試呢?什么給予?
查看完整描述

3 回答

?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊


名稱置亂用于確保子類不會(huì)意外地覆蓋其超類的私有方法和屬性。它的設(shè)計(jì)并不是為了防止故意從外部進(jìn)入。


例如:


>>> class Foo(object):

...     def __init__(self):

...         self.__baz = 42

...     def foo(self):

...         print self.__baz

...     

>>> class Bar(Foo):

...     def __init__(self):

...         super(Bar, self).__init__()

...         self.__baz = 21

...     def bar(self):

...         print self.__baz

...

>>> x = Bar()

>>> x.foo()

42

>>> x.bar()

21

>>> print x.__dict__

{'_Bar__baz': 21, '_Foo__baz': 42}

當(dāng)然,如果兩個(gè)不同的類有相同的名稱,它就會(huì)分解。


查看完整回答
反對 回復(fù) 2019-06-23
?
qq_笑_17

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊

私有函數(shù)示例

import reimport inspectclass MyClass :

    def __init__(self) :
        pass

    def private_function ( self ) :
        try :
            function_call = inspect.stack()[1][4][0].strip()

            # See if the function_call has "self." in the begining
            matched = re.match( '^self\.', function_call )
            if not matched :
                print 'This is Private Function, Go Away'
                return
        except :
            print 'This is Private Function, Go Away'
            return

        # This is the real Function, only accessible inside class #
        print 'Hey, Welcome in to function'

    def public_function ( self ) :
        # i can call private function from inside the class
        self.private_function()### End ###


查看完整回答
反對 回復(fù) 2019-06-23
?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

當(dāng)我第一次從Java到Python時(shí)痛恨這,這個(gè)。把我嚇?biāo)懒恕?/trans>

今天也許只有一件事我最愛關(guān)于Python。

我喜歡在一個(gè)平臺(tái)上,人們互相信任,不覺得他們需要在代碼周圍建造無法穿透的墻。在強(qiáng)封裝的語言中,如果一個(gè)API有一個(gè)bug,并且您已經(jīng)知道出了什么問題,那么您可能仍然無法繞過它,因?yàn)樗璧姆椒ㄊ撬接械?。在Python中,態(tài)度是:“當(dāng)然”。如果你認(rèn)為你了解情況,也許你甚至讀過,那我們只能說“祝你好運(yùn)!”

請記住,封裝與“安全性”或讓孩子遠(yuǎn)離草坪的關(guān)系并不弱。這只是另一種模式,應(yīng)該用來使代碼庫更容易理解。


查看完整回答
反對 回復(fù) 2019-06-23
  • 3 回答
  • 0 關(guān)注
  • 1013 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)