<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] [參考鏈接](http://www.cnblogs.com/linhaifeng/articles/6204014.html) 首先需要知道,`__xx__`這種以`__`開頭并以`__`結尾的方法,會在(滿足條件)某種情況下由python自動執行的方法. ## isinstance和issubclass **isinstance(obj,cls) 檢查obj是否是類 cls 的對象** ~~~ class Foo(object): pass obj = Foo() isinstance(obj, Foo) ~~~ **issubclass(sub, super) 檢查sub類是否是 super 類的派生類** ~~~ class Foo(object): pass class Bar(Foo): pass issubclass(Bar, Foo) ~~~ ## `__item__`字典系列 item系列方法,是將對象模擬為字典來使用,即將原來的obj.name的調用方式,改為obj['name'] getitme用于獲取對象屬性,setitem用于設置對象屬性,delitem用于刪除對象屬性 * __getitem__:字典方式獲取屬性 ``` class foo: def __init__(self,name): self.name=name def __getitem__(self, item): return self.__dict__.get(item) obj=foo('noah') print(obj.__dict__) #結果:{'name': 'noah'} print(obj.name) #結果:noah print(obj['name']) #結果:noah ``` * __setitem__:字典方式設置屬性 ~~~ class foo: def __init__(self,name): self.name=name def __setitem__(self, key, value): self.__dict__[key]=value obj=foo('noah') obj['name']='luogang' print(obj.__dict__) #結果:{'name': 'luogang'} ~~~ * __delitem__:字典方式刪除屬性 ~~~ class foo: def __init__(self,name): self.name=name def __delitem__(self, key): del self.__dict__[key] obj=foo('noah') del obj['name'] print(obj.__dict__) #結果:{} ~~~ * 三個方法一起使用 ``` class Foo: def __init__(self,name): self.name=name def __getitem__(self, item): #item='namexxx' return self.__dict__.get(item) def __setitem__(self, key, value): self.__dict__[key]=value def __delitem__(self, key): del self.__dict__[key] obj=Foo('noah') print(obj.__dict__) ``` ## `__str__`打印提示方法: `__str__`方法會在執行打印的時候執行,并返回一個字符串數據 正常情況下,打印一個自定義的類的實例化對象,會顯示該對象的內存地址. 而dict這類自帶的類的實例化對象,打印的時候,是打印字典對象中的數據,區別如下: ~~~ class foo: name='noah' A=foo() print(A) #結果:<__main__.foo object at 0x00000184B5399748> B=dict({'name':'noah'}) print(B) #結果:{'name': 'noah'} ~~~ 自定義類的對象的打印結果,給不了任何有用的信息,如果想像dict一樣打印有用的提示信息,就需要用到`__str__` ``` class People: def __init__(self,name,age): self.name=name self.age=age def __str__(self): return '<name:%s,age:%s>' %(self.name,self.age) obj=People('egon',18) print(obj) #相當于執行print(obj.__str__()) #結果: <name:egon,age:18> ``` ## `__del__`資源回收方法 在對象被刪除的時候會先自動執行此方法,然后再刪除對象,可以用來回收和對象相關的系統資源 例如`f=open('settings.py')`命令: 此做了兩個操作,一是定義了python變量`f`,二是請求操作系統打開了文件`settings.py`,當程序運行結束時,python定義的變量會自動回收,但操作系統打開的文件卻沒有回收,所以需要執行`f.close()`方法關閉文件 方法打開文件,后,如果出現運行完成,python會自動` ``` class Open: def __init__(self,filename): print('open file.......') self.filename=filename def __del__(self): print('回收操作系統資源:self.close()') f=Open('settings.py') print('----main------') #程序運行結束后自動執行del f.__del__() #運行結果: open file....... ----main------ 回收操作系統資源:self.close() ``` ## `__call__方法` call方法可以使實例化的對象能被調用,也就是對象在被點用時執行call方法 默認情況下,實例化好的對象是不能被調用的,如下 ~~~ class Foo: pass obj=Foo() obj() #輸出:TypeError: 'people' object is not callable ~~~ 如果想讓obj能再次被調用,就需要添加call方法,如下 ~~~ class Foo: pass def __call__(self, *args, **kwargs): print(self) print(args) print(kwargs) obj=Foo() obj('noah','luo',age=1,sex=2) #輸出: <__main__.Foo object at 0x0000016DFF81A6A0> ('noah', 'luo') {'age': 1, 'sex': 2} ~~~ 具體調用obj時想要達到什么效果,就修改call方法就可以了
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看