python實(shí)現(xiàn)裝飾器、描述符
概要
本人python理論知識(shí)遠(yuǎn)達(dá)不到傳授級(jí)別,寫(xiě)文章主要目的是自我總結(jié),并不能照顧所有人,請(qǐng)見(jiàn)諒,文章結(jié)尾貼有相關(guān)鏈接可以作為補(bǔ)充
全文分為三個(gè)部分裝飾器理論知識(shí)、裝飾器應(yīng)用、裝飾器延申
- 裝飾理基礎(chǔ):無(wú)參裝飾器、有參裝飾器、functiontools、裝飾器鏈
- 裝飾器進(jìn)階:property、staticmethod、classmethod源碼分析(python代碼實(shí)現(xiàn))
裝飾器基礎(chǔ)
無(wú)參裝飾器
'''
假定有一個(gè)需求是:打印程序函數(shù)運(yùn)行順序
此案例打印的結(jié)果為:
foo1 function is starting
foo2 function is starting
'''
from functools import wraps
def NoParamDec(func):
#函數(shù)在被裝飾器裝時(shí)后,其函數(shù)屬性也會(huì)改變,wraps作用就是保證被裝飾函數(shù)屬性不變
@wraps(func)
def warpper(*args, **kwargs):
print('{} function is starting'.format(func.__name__))
return func(*args, **kwargs)
return warpper
#python黑魔法省略了NoParamDec=NoParamDec(foo1)
@NoParamDec
def foo1():
foo2()
@NoParamDec
def foo2():
pass
if __name__ == "__main__":
foo1()
有參裝飾器
'''
假定有一個(gè)需求是:檢查函數(shù)參數(shù)的類型,只允許匹配正確的函數(shù)通過(guò)程序
此案例打印結(jié)果為:
('a', 'b', 'c')
-----------------------分割線------------------------
ERROS!!!!b must be <class 'str'>
ERROS!!!!c must be <class 'str'>
('a', 2, ['b', 'd'])
'''
from functools import wraps
from inspect import signature
def typeAssert(*args, **kwargs):
deco_args = args
deco_kwargs = kwargs
def factor(func):
#python標(biāo)準(zhǔn)模塊類,可以用來(lái)檢查函數(shù)參數(shù)類型,只允許特定類型通過(guò)
sig = signature(func)
#將函數(shù)形式參數(shù)和規(guī)定類型進(jìn)行綁定
check_bind_args = sig.bind_partial(*deco_args, **deco_kwargs).arguments
@wraps(func)
def wrapper(*args, **kwargs):
#將實(shí)際參數(shù)值和形式參數(shù)進(jìn)行綁定
wrapper_bind_args = sig.bind(*args, **kwargs).arguments.items()
for name, obj in wrapper_bind_args:
#遍歷判斷是否實(shí)際參數(shù)值是規(guī)定參數(shù)的實(shí)例
if not isinstance(obj, check_bind_args[name]):
try:
raise TypeError('ERROS!!!!{arg} must be {obj} '.format(**{'arg': name, 'obj': check_bind_args[name]}))
except Exception as e:
print(e)
return func(*args, **kwargs)
return wrapper
return factor
@typeAssert(str, str, str)
def inspect_type(a, b, c):
return (a, b, c)
if __name__ == "__main__":
print(inspect_type('a', 'b', 'c'))
print('{:-^50}'.format('分割線'))
print(inspect_type('a', 2, ['b', 'd']))
裝飾器鏈
'''
假定有一個(gè)需求是:
輸入類似代碼:
@makebold
@makeitalic
def say():
return "Hello"
輸出:
<b><i>Hello</i></b>
'''
from functools import wraps
def html_deco(tag):
def decorator(fn):
@wraps(fn)
def wrapped(*args, **kwargs):
return '<{tag}>{fn_result}<{tag}>'.format(**{'tag': tag, 'fn_result': fn(*args, **kwargs)})
return wrapped
return decorator
@html_deco('b')
@html_deco('i')
def greet(whom=''):
# 等價(jià)于 geet=html_deco('b')(html_deco('i)(geet))
return 'Hello' + (' ' + whom) if whom else ''
if __name__ == "__main__":
print(greet('world')) # -> <b><i>Hello world</i></b>
裝飾器進(jìn)階
property 原理
通常,描述符是具有“綁定行為”的對(duì)象屬性,其屬性訪問(wèn)已經(jīng)被描述符協(xié)議中的方法覆蓋。這些方法是__get__()、__set__()和__delete__()。如果一個(gè)對(duì)象定義這些方法中的任何一個(gè),它被稱為一個(gè)描述符。如果對(duì)象定義__get__()和__set__(),則它被認(rèn)為是數(shù)據(jù)描述符。僅定義__get__()的描述器稱為非數(shù)據(jù)描述符(它們通常用于方法,但是其他用途也是可能的)。
屬性查找優(yōu)先級(jí)為:
- 類屬性
- 數(shù)據(jù)描述符
- 實(shí)例屬性
- 非數(shù)據(jù)描述符
- 默認(rèn)為_(kāi)_getattr__()
class Property(object):
'''
內(nèi)部property是用c實(shí)現(xiàn)的,這里用python模擬實(shí)現(xiàn)property功能
代碼參考官方doc文檔
'''
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
self.__doc__ = doc
def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise (AttributeError, "unreadable attribute")
print('self={},obj={},objtype={}'.format(self,obj,objtype))
return self.fget(obj)
def __set__(self, obj, value):
if self.fset is None:
raise (AttributeError, "can't set attribute")
self.fset(obj, value)
def __delete__(self, obj):
if self.fdel is None:
raise (AttributeError, "can't delete attribute")
self.fdel(obj)
def getter(self, fget):
return type(self)(fget, self.fset, self.fdel, self.__doc__)
def setter(self, fset):
return type(self)(self.fget, fset, self.fdel, self.__doc__)
def deleter(self, fdel):
return type(self)(self.fget, self.fset, fdel, self.__doc__)
class Student( object ):
@Property
def score( self ):
return self._score
@score.setter
def score( self, val ):
if not isinstance( val, int ):
raise ValueError( 'score must be an integer!' )
if val > 100 or val < 0:
raise ValueError( 'score must between 0 ~ 100!' )
self._score = val
if __name__ == "__main__":
s = Student()
s.score = 60
s.score
staticmethod 原理
@staticmethod means: when this method is called, we don't pass an instance of the class to it (as we normally do with methods). This means you can put a function inside a class but you can't access the instance of that class (this is useful when your method does not use the instance).
class StaticMethod(object):
"python代碼實(shí)現(xiàn)staticmethod原理"
def __init__(self, f):
self.f = f
def __get__(self, obj, objtype=None):
return self.f
class E(object):
#StaticMethod=StaticMethod(f)
@StaticMethod
def f( x):
return x
if __name__ == "__main__":
print(E.f('staticMethod Test'))
classmethod
@staticmethod means: when this method is called, we don't pass an instance of the class to it (as we normally do with methods). This means you can put a function inside a class but you can't access the instance of that class (this is useful when your method does not use the instance).
class ClassMethod(object):
"python代碼實(shí)現(xiàn)classmethod原理"
def __init__(self, f):
self.f = f
def __get__(self, obj, klass=None):
if klass is None:
klass = type(obj)
def newfunc(*args):
return self.f(klass, *args)
return newfunc
class E(object):
#ClassMethod=ClassMethod(f)
@ClassMethod
def f(cls,x):
return x
if __name__ == "__main__":
print(E().f('classMethod Test'))
相關(guān)文章
我用Python給班主任寫(xiě)了一個(gè)自動(dòng)閱卷腳本(附源碼)
這篇文章主要介紹了如何用Python給寫(xiě)了一個(gè)自動(dòng)閱卷腳本,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Python實(shí)現(xiàn)繁體中文與簡(jiǎn)體中文相互轉(zhuǎn)換的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)繁體中文與簡(jiǎn)體中文相互轉(zhuǎn)換的方法,涉及Python基于第三方模塊進(jìn)行編碼轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
詳解python的argpare和click模塊小結(jié)
這篇文章主要介紹了詳解python的argpare和click模塊小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
PyCharm中如何直接使用Anaconda已安裝的庫(kù)
這篇文章主要介紹了PyCharm中如何直接使用Anaconda已安裝的庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Python優(yōu)先隊(duì)列實(shí)現(xiàn)方法示例
這篇文章主要介紹了Python優(yōu)先隊(duì)列實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python優(yōu)先隊(duì)列的具體定義與使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-09-09
利用Python實(shí)現(xiàn)一鍵將頭像轉(zhuǎn)成動(dòng)漫風(fēng)
小編今天將為大家詳細(xì)介紹如何利用Python語(yǔ)言制作一個(gè)UI界面,大家可以通過(guò)一鍵點(diǎn)擊就實(shí)現(xiàn)頭像照片轉(zhuǎn)化成動(dòng)漫風(fēng)格的功能,感興趣的可以動(dòng)手嘗試一下2022-07-07
Python 字符串轉(zhuǎn)換為整形和浮點(diǎn)類型的方法
今天小編就為大家分享一篇Python 字符串轉(zhuǎn)換為整形和浮點(diǎn)類型的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python實(shí)現(xiàn)電視里的5毛特效實(shí)例代碼詳解
這篇文章主要介紹了Python實(shí)現(xiàn)了電視里的5毛特效,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Python Selenium中等待設(shè)置的實(shí)現(xiàn)
本文主要介紹了Python Selenium中等待設(shè)置的實(shí)現(xiàn),過(guò)詳實(shí)的示例代碼,深入介紹了顯式等待、隱式等待、自定義等待條件、多重等待條件、頁(yè)面加載狀態(tài)的等待、元素存在與可見(jiàn)性等待、Fluent等待以及異步JavaScript加載的等待,感興趣的可以了解一下2023-12-12
Python Selenium中常用的元素定位方法總結(jié)
在Web自動(dòng)化測(cè)試中,元素定位是一項(xiàng)非常重要的技術(shù),Python Selenium提供了各種元素定位方法,可以幫助我們定位頁(yè)面上的元素并與之交互,本文將詳細(xì)介紹Python Selenium中常用的元素定位方法,并提供實(shí)例代碼,需要的朋友可以參考下2023-11-11

