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

Python的裝飾器詳情介紹

 更新時(shí)間:2022年03月01日 10:17:35   作者:Mar丶流年  
這篇文章主要介紹了Python的裝飾器詳情,主要介紹裝飾器定以、調(diào)用方式等相關(guān)內(nèi)容,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助

1.定義及使用

例1:裝飾器定義:

      def 裝飾器函數(shù)(外部函數(shù)):
            def 內(nèi)聯(lián)函數(shù)(*args,**kwargs):
                ...前置裝飾...
                外部函數(shù)(*args,**kwargs)
                ...后置裝飾...
            return 內(nèi)聯(lián)函數(shù)

 例2:裝飾器兩種調(diào)用方式

  •  第一種:裝飾器函數(shù)(外部函數(shù))(參數(shù)1,參數(shù)2......)
  •  第二種:定義時(shí)通過 @裝飾器函數(shù)名 綁定 外部函數(shù)(外部函數(shù)調(diào)用時(shí)觸發(fā))
# coding:utf-8:

if __name__ == '__main__':

? ? # 例1 裝飾器定義
? ? # 裝飾器函數(shù) 外部函數(shù)func
? ? def decorator(func):

? ? ? ? # 內(nèi)聯(lián)函數(shù) 進(jìn)行裝飾
? ? ? ? # *args 將 參數(shù)1,參數(shù)2...... 變?yōu)?(參數(shù)1,參數(shù)2.......)
? ? ? ? # **kwargs 將 參數(shù)3=參數(shù)值3,參數(shù)4=參數(shù)值4...... 變?yōu)?{'參數(shù)3':參數(shù)值3,'參數(shù)4':'參數(shù)值4'......}
? ? ? ? # *args,**kwargs 將 參數(shù)1,參數(shù)2......參數(shù)3=參數(shù)值3,參數(shù)4=參數(shù)值4...... 變?yōu)?(參數(shù)1,參數(shù)2.......),{'參數(shù)3':參數(shù)值3,'參數(shù)4':'參數(shù)值4'......}
? ? ? ? def inline(*args, **kwargs):
? ? ? ? ? ? # *args,**kwargs 將參數(shù)還原
? ? ? ? ? ? # 將 (參數(shù)1,參數(shù)2.......),{'參數(shù)3':參數(shù)值3,'參數(shù)4':'參數(shù)值4'......} 變?yōu)?參數(shù)1,參數(shù)2......參數(shù)3=參數(shù)值3,參數(shù)4=參數(shù)值4......
? ? ? ? ? ? name = func(*args, **kwargs)
? ? ? ? ? ? print(f'name is {name}')

? ? ? ? # return 內(nèi)聯(lián)函數(shù)
? ? ? ? return inline

? ? def talk(name):
? ? ? ? return name

? ? # 例2 裝飾器的兩種調(diào)用方式
? ? # 第一種 裝飾器函數(shù)(外部函數(shù))(參數(shù)1,參數(shù)2......)
? ? decorator(talk)('xie') ?# name is xie

? ? # 第二種 @裝飾器函數(shù)名 綁定 外部函數(shù)
? ? @decorator
? ? def see(name):
? ? ? ? return name
? ? # 調(diào)用時(shí)觸發(fā)裝飾器
? ? see('xie') ?# name is xie

2.@classmethod

  •   1.被@classmethod裝飾的類方法可以通過class.方法(參數(shù)1,參數(shù)2......)調(diào)用
  •     2.但是定義函數(shù)時(shí) self 需要變成 cls
  •     3.其內(nèi)部不能調(diào)用類的普通方法(無裝飾器修飾的方法),可以調(diào)用@classmethod,@staticmethod裝飾的方法
  •     4.能訪問類的屬性 
  •     5.普通類中能通過self調(diào)用@classmethod裝飾的方法
# coding:utf-8:

if __name__ == '__main__':


? ? class A(object):
? ? ? ? __name = 'python'

? ? ? ? # 普通方法
? ? ? ? def talk(self):
? ? ? ? ? ? print(self.__name)
? ? ? ? ? ? # self.see() 普通類中能通過self調(diào)用@classmethod裝飾的方法

? ? ? ? # 被@classmethod裝飾的類方法可以通過class.方法(參數(shù)1,參數(shù)2......)調(diào)用
? ? ? ? # 但是定義函數(shù)時(shí) self 需要變成 cls
? ? ? ? @classmethod
? ? ? ? def see(cls, description='good'):
? ? ? ? ? ? # cls.talk() Error 不能調(diào)用類的普通方法(非@classmethod,@staticmethod修飾的方法)
? ? ? ? ? ? # cls.look() 可以調(diào)用@classmethod裝飾的方法
? ? ? ? ? ? # cls.jump() 可以調(diào)用@staticmethod裝飾的方法
? ? ? ? ? ? # 能訪問類的屬性
? ? ? ? ? ? print(f'{cls .__name} is {description}')

? ? ? ? @classmethod
? ? ? ? def look(cls):
? ? ? ? ? ? print(f'I like {cls.__name}')

? ? ? ? @staticmethod
? ? ? ? def jump():
? ? ? ? ? ? print(f'I am jump')

? ? a = A()
? ? a.talk() ?# python
? ? # A.talk() Error 不能通過class.方法(參數(shù)1,參數(shù)2......)調(diào)用
? ? a.see() ?# python is good

? ? # 通過class.方法(參數(shù)1,參數(shù)2......)調(diào)用
? ? A.see() ?# python is good

@staticmethod

  •    1. 被@staticmethod裝飾的類方法可以通過class.方法(參數(shù)1,參數(shù)2......)調(diào)用
  •     2. 但是定義函數(shù)時(shí) 無須self和cls
  •     3. 由于其無self,cls注定其無法訪問類屬性&調(diào)用類方法
  •     4. 在類的普通方法中可以通過self調(diào)用@staticmethod裝飾的方法
# coding:utf-8:

if __name__ == '__main__':
? ? '''
? ? ? '''

? ? class B(object):
? ? ? ? __name = 'php'

? ? ? ? def talk(self):
? ? ? ? ? ? # 可以通過self調(diào)用@staticmethod裝飾的方法
? ? ? ? ? ? self.see(self.__name)

? ? ? ? # 無須self,cls
? ? ? ? @staticmethod
? ? ? ? def see(description='good'):
? ? ? ? ? ? print(f'description is {description}')


? ? B.see() ?# description is good
? ? B.see('ok') ?# description is ok
? ? B().talk() ?# description is php

@property

  • 1.@property裝飾的函數(shù)被用來代替類中與函數(shù)名相同的屬性

      定義: @property
            def 屬性名(self):
                .......

  •   2.被@property裝飾器代替的屬性,無法通過object.屬性名=屬性值進(jìn)行賦值(除非使用了@屬性名.setter裝飾器):

      定義: @屬性名.setter
            def 屬性名(self,屬性值):
                ......  

  • 3.被@property修飾的函數(shù)不能在外部通過object.函數(shù)名()調(diào)用,只能object.函數(shù)名 當(dāng)做屬性
  • 4.只有被@property代替了的屬性才能使用@屬性名.setter 裝飾器
  • 5. __setattr__ 的優(yōu)先級(jí)高于 @屬性名.setter裝飾器的優(yōu)先級(jí)
# coding:utf-8:

if __name__ == '__main__':
? ? '''
? ?
? ? '''
? ? class A(object):
? ? ? ? __name = 'python'
? ? ? ? sex = 'man'

? ? ? ? # 不能設(shè)置成私有
? ? ? ? # @property裝飾的函數(shù)被用來代替類中與函數(shù)名相同的屬性
? ? ? ? # 這個(gè)代替了name屬性
? ? ? ? @property
? ? ? ? def name(self):
? ? ? ? ? ? return self.__name

@property

def sex(self):
? ? ? ? ? ? return 'woman'

? ? ? ? # 解決被替代屬性的 object.屬性=屬性值 賦值問題
? ? ? ? # 配合@property裝飾器使用,只有被@property代替了的屬性才能使用@屬性名.setter 裝飾器
? ? ? ? @name.setter
? ? ? ? def name(self, value):
? ? ? ? ? ? print(f'value is {value}')

? ? ? ? # __setattr__ 的優(yōu)先級(jí)高于 @屬性名.setter裝飾器的優(yōu)先級(jí)
? ? ? ? # def __setattr__(self, key, value):
? ? ? ? # ? ? print(f'key is {key}, value is {value}')


? ? a = A()
? ? print(a.name) ?# python
? ? # print(a.name()) Error 被@property修飾的函數(shù)不能在外部通過object.函數(shù)名()調(diào)用,只能object.函數(shù)名 當(dāng)做屬性

? ? # 被@property代替了
? ? print(a.sex) ?# 是 woman 不是 man

? ? # a.sex = 'man' Error 被代替的屬性,不能通過object.屬性名 = 屬性值 進(jìn)行賦值,除非有@屬性名.setter裝飾
? ? a.name = 'python3.7' ?# value is python3.7

到此這篇關(guān)于Python的裝飾器詳情介紹的文章就介紹到這了,更多相關(guān)Python裝飾器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Python提取PDF表格到Excel文件的操作步驟

    使用Python提取PDF表格到Excel文件的操作步驟

    在對(duì)PDF中的表格進(jìn)行再利用時(shí),除了直接將PDF文檔轉(zhuǎn)換為Excel文件,我們還可以提取PDF文檔中的表格數(shù)據(jù)并寫入Excel工作表,本文將介紹如何使用Python提取PDF文檔中的表格并寫入Excel文件中,需要的朋友可以參考下
    2024-09-09
  • pandas 實(shí)現(xiàn)某一列分組,其他列合并成list

    pandas 實(shí)現(xiàn)某一列分組,其他列合并成list

    這篇文章主要介紹了pandas 實(shí)現(xiàn)某一列分組,其他列合并成list的案例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • python3抓取中文網(wǎng)頁的方法

    python3抓取中文網(wǎng)頁的方法

    這篇文章主要介紹了python3抓取中文網(wǎng)頁的方法,實(shí)例分析了Python3頁面抓取及編碼轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • Python生成隨機(jī)數(shù)字和字符詳情

    Python生成隨機(jī)數(shù)字和字符詳情

    這篇文章主要介紹了Python生成隨機(jī)數(shù)字和字符詳情,random是python自帶庫,使用前導(dǎo)入import?random即可,更多相關(guān)內(nèi)容需要的朋友可以參考一下
    2022-07-07
  • 教你編譯pjsip源碼的方法

    教你編譯pjsip源碼的方法

    通過本文教大家如何編譯pjsip源碼,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-10-10
  • python多進(jìn)程提取處理大量文本的關(guān)鍵詞方法

    python多進(jìn)程提取處理大量文本的關(guān)鍵詞方法

    今天小編就為大家分享一篇python多進(jìn)程提取處理大量文本的關(guān)鍵詞方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python入門教程(二十六)Python的模塊

    Python入門教程(二十六)Python的模塊

    這篇文章主要介紹了Python入門教程(二十六)Python的模塊,Python是一門非常強(qiáng)大好用的語言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下
    2023-04-04
  • 如何在python中使用openpyxl庫讀寫Excel.xlsx文件(有參考列程)

    如何在python中使用openpyxl庫讀寫Excel.xlsx文件(有參考列程)

    這篇文章主要給大家介紹了關(guān)于如何在python中使用openpyxl庫讀寫Excel.xlsx文件的相關(guān)資料,openpyxl是一個(gè)第三方庫,可以處理xlsx格式的Excel文件,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • 淺談機(jī)器學(xué)習(xí)需要的了解的十大算法

    淺談機(jī)器學(xué)習(xí)需要的了解的十大算法

    這篇文章主要介紹了淺談機(jī)器學(xué)習(xí)需要的了解的十大算法,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Python與xlwings黃金組合處理Excel各種數(shù)據(jù)和自動(dòng)化任務(wù)

    Python與xlwings黃金組合處理Excel各種數(shù)據(jù)和自動(dòng)化任務(wù)

    這篇文章主要為大家介紹了Python與xlwings黃金組合處理Excel各種數(shù)據(jù)和自動(dòng)化任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>
    2023-12-12

最新評(píng)論

栾川县| 景洪市| 鹿泉市| 富阳市| 鄱阳县| 阳新县| 子洲县| 搜索| 大埔县| 界首市| 茂名市| 元朗区| 兴城市| 佛山市| 绥滨县| 甘南县| 邹城市| 石林| 灵寿县| 札达县| 洛宁县| 伊金霍洛旗| 施秉县| 修文县| 清涧县| 东阳市| 临颍县| 乌兰浩特市| 马龙县| 弥渡县| 大安市| 巩义市| 乌恰县| 萍乡市| 礼泉县| 牙克石市| 本溪市| 淮滨县| 历史| 南丰县| 安宁市|