python的getattr和getattribute攔截內(nèi)置操作實(shí)現(xiàn)
1.1 特性描述符getattr(bute)管理屬性比較
描述
特性和描述符,管理屬性時(shí),實(shí)例屬性用前單下劃線開頭,self._attr。
__getattr__(),管理屬性時(shí),未定義屬性的點(diǎn)號運(yùn)算返回已定義屬性的點(diǎn)號運(yùn)算。
__getattribute__(),管理屬性時(shí),用object.__getattribute__(self,name,value),避免循環(huán)。
示例
>>> class SquareCubePro: ''' property 特性計(jì)算平方和立方 前下化線開頭屬性名存儲(chǔ)基礎(chǔ)數(shù)據(jù) 賦值為特性的屬性名不帶下劃線 self._square和square=property: 實(shí)例屬性和特性屬性不能同名,避免循環(huán) ''' def __init__(self,sq,cu): self._square=sq self._cube=cu def getSquare(self): return self._square**2 def setSquare(self,value): self._square=value square=property(getSquare,setSquare) def getCube(self): return self._cube**3 cube=property(getCube) >>> scp=SquareCubePro(2,3) >>> scp.square,scp.cube (4, 27) >>> scp=SquareCubePro(3,5) >>> scp.square,scp.cube (9, 125) >>> scp.square=5 >>> scp.square 25 >>> class SquareDesc: ''' Descriptor 描述符計(jì)算平方和立方 基礎(chǔ)數(shù)據(jù)存儲(chǔ)在客戶類實(shí)例的屬性上 ''' def __get__(self,instance,owner): return instance._square**2 def __set__(self,instance,value): instance._square=value >>> class CubeDesc: def __get__(self,instance,owner): return instance._cube**3 >>> class SquareCubeDesc: sq=SquareDesc() cu=CubeDesc() def __init__(self,sq,cu): self._square=sq self._cube=cu >>> scd=SquareCubeDesc(3,5) >>> scd.sq,scd.cu (9, 125) >>> scd.sq=5 >>> scd.sq 25 >>> class SquareCubeGetA: ''' 重載 __getattr__ 計(jì)算平方和立方 ''' def __init__(self,sq,cu): self._square=sq self._cube=cu def __getattr__(self,name): if name=='square': return self._square**2 elif name=='cube': return self._cube**3 else: raise TypeError('屬性錯(cuò)誤:',name) def __setattr__(self,name,value): if name=='square': self.__dict__['_square']=value else: self.__dict__[name]=value >>> scga=SquareCubeGetA(3,5) >>> scga.square,scga.cube (9, 125) >>> scga.square=5 >>> scga.square 25 >>> class SquareCubeGetAB: ''' 重載 __getattribute__ 計(jì)算平方和立方 ''' def __init__(self,sq,cu): self._square=sq self._cube=cu def __getattribute__(self,name): if name=='square': return object.__getattribute__(self,'_square')**2 elif name=='cube': return object.__getattribute__(self,'_cube')**3 else: return object.__getattribute__(self,name) def __setattr__(self,name,value): if name=='square': self.__dict__['_square']=value else: self.__dict__[name]=value >>> scgab=SquareCubeGetAB(3,5) >>> scgab.square,scgab.cube (9, 125) >>> scgab.square=5 >>> scgab.square 25
1.2 getattr和getattribute攔截內(nèi)置操作
python內(nèi)置操作和對應(yīng)方法。
NO | 內(nèi)置操作 | 對應(yīng)方法 |
---|---|---|
1 | 索引操作[i] | __getitem__ |
2 | 加法(連接)操作+ | __coerce__ __add__ |
3 | 括號調(diào)用() | __call__ |
4 | 打印print() | __str__ |
__coerce__:表示強(qiáng)制類型轉(zhuǎn)換,使用加法(或連接)操作+時(shí),不同類型會(huì)觸發(fā)類型轉(zhuǎn)換或者報(bào)錯(cuò)。
1.2.1 S.center(width[, fillchar]) -> string
用法
S.center(width[, fillchar]) -> string
描述
python的s.center()使字符串居中對齊。
S:字符串;
width:字符串寬度;
fillchar:填充字符,字符串長度小于width時(shí)生效,否則不生效。
示例
>>> strL=['MyGetAttr','MyGetAttribute'] >>> for s in strL: print('\n'+s.center(50,'=')) print('test center') ====================MyGetAttr===================== test center ==================MyGetAttribute================== test center
1.2.2 testgetattr.py
# encoding:utf-8 class MyGetAttr: name='梯閱線條' def __init__(self): self.url='tyxt.work' def __len__(self): print('__len__:9555') return 9555 def __getattr__(self,attr): print('__getattr__:'+attr) if attr == '__str__': return lambda *args:'[GetAttr str]' else: return lambda *args:None class MyGetAttribute(object): name='梯閱線條' def __init__(self): self.url='tyxt.work' def __len__(self): print('__len__:9555') return 9555 def __getattribute__(self,attr): print('__getattribute__:'+attr) if attr == '__str__': return lambda *args:'[Getattribute str]' else: return lambda *args:None if __name__=='__main__': import sys print('python '+sys.version.split()[0]) for C in MyGetAttr,MyGetAttribute: print('\n'+C.__name__.center(50,'=')) c1=C() c1.name c1.url c1.tel len(c1) try: c1[0] except: print('fail []') try: c1+99 except: print('fail +') try: c1() except: print('fail ()') c1.__call__() print(c1.__str__()) print(c1)
1.2.3 python2.x的getattr和getattribute攔截內(nèi)置操作
python2.x的__getattr__()攔截未定義屬性操作,包括當(dāng)前類內(nèi)未定義的seq[i]、+、()、print()等內(nèi)置操作。
python2.x的__getattribute__()攔截全部屬性的點(diǎn)號運(yùn)算、賦值運(yùn)算、刪除屬性,不攔截當(dāng)前類內(nèi)未定義的seq[i]、+、()、print()等內(nèi)置操作。
NO | 調(diào)用方式 | 是否被__getattr__()攔截 | 是否被__ getattribute __()攔截 |
---|---|---|---|
1 | print(實(shí)例名) | 被攔截 | 不被攔截 |
2 | 實(shí)例名.__str__() | 被攔截 | 被攔截 |
3 | 實(shí)例名() | 被攔截 | 不被攔截 |
4 | 實(shí)例名.__call__() | 被攔截 | 被攔截 |
5 | []、+ | 被攔截 | 不被攔截 |
在cmd執(zhí)行結(jié)果如下:
C:\Users\Administrator>D:\Python27\python.exe E:\documents\F盤\testgetattr.py python 2.7.18 ====================MyGetAttr===================== __getattr__:tel __len__:9555 __getattr__:__getitem__ __getattr__:__coerce__ __getattr__:__add__ __getattr__:__call__ __getattr__:__call__ __getattr__:__str__ [GetAttr str] __getattr__:__str__ [GetAttr str] ==================MyGetAttribute================== __getattribute__:name __getattribute__:url __getattribute__:tel __len__:9555 fail [] fail + fail () __getattribute__:__call__ __getattribute__:__str__ [Getattribute str] <__main__.MyGetAttribute object at 0x00000000034B7C88>
1.2.4 python3.x的getattr和getattribute攔截內(nèi)置操作
python3.x的__getattr__()攔截未定義屬性操作,不攔截當(dāng)前類內(nèi)未定義的seq[i]、+、()、print()等內(nèi)置操作。
python3.x的__getattribute__()攔截全部屬性的點(diǎn)號運(yùn)算、賦值運(yùn)算、刪除屬性,不攔截當(dāng)前類內(nèi)未定義的seq[i]、+、()、print()等內(nèi)置操作。
python3.x中,MyGetAttr未定義__str__(),但都未被__getattr__()攔截,因?yàn)閺膐bject繼承了__str__()方法,所以不會(huì)被攔截。通過hasattr(MyGetAttr,‘__str__’)返回True驗(yàn)證。
python3.x中,MyGetAttribute未定義__str__(),通過print()打印不會(huì)被攔截,顯式調(diào)用會(huì)被攔截。
python3.x中,調(diào)用小括號(),即__call__,不會(huì)被__getattr__()和__getattribute__()攔截,顯式調(diào)用會(huì)被攔截。
NO | 調(diào)用方式 | 是否被__getattr__()攔截 | 是否被__ getattribute __()攔截 |
---|---|---|---|
1 | print(實(shí)例名) | 不被攔截 | 不被攔截 |
2 | 實(shí)例名.__str__() | 不被攔截 | 被攔截 |
3 | 實(shí)例名() | 不被攔截 | 不被攔截 |
4 | 實(shí)例名.__call__() | 被攔截 | 被攔截 |
5 | []、+ | 不被攔截 | 不被攔截 |
在cmd執(zhí)行結(jié)果如下:
C:\Users\Administrator>D:\Python3\python.exe E:\documents\F盤\testgetattr.py python 3.7.8 ====================MyGetAttr===================== __getattr__:tel __len__:9555 fail [] fail + fail () __getattr__:__call__ <__main__.MyGetAttr object at 0x01648E10> <__main__.MyGetAttr object at 0x01648E10> ==================MyGetAttribute================== __getattribute__:name __getattribute__:url __getattribute__:tel __len__:9555 fail [] fail + fail () __getattribute__:__call__ __getattribute__:__str__ [Getattribute str] <__main__.MyGetAttribute object at 0x01648F50>
到此這篇關(guān)于python的getattr和getattribute攔截內(nèi)置操作實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python的getattr和getattribute攔截內(nèi)置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
分享8?個(gè)常用pandas的?index設(shè)置
這篇文章主要介紹了分享8?個(gè)常用pandas的?index設(shè)置,pandas?中的?index?是行索引或行標(biāo)簽。行標(biāo)簽可以說是?pandas?的靈魂一簽,支撐了?pandas?很多強(qiáng)大的業(yè)務(wù)功能,比如多個(gè)數(shù)據(jù)框的?join,?merge?操作,自動(dòng)對齊等,下面來看看文章得具體介紹吧2021-12-12完美解決Pycharm中matplotlib畫圖中文亂碼問題
這篇文章主要介紹了完美解決Pycharm中matplotlib畫圖中文亂碼問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01基于Python實(shí)現(xiàn)多人聊天室的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)多人聊天室功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2025-02-02python調(diào)用其他文件函數(shù)或類的示例
今天小編就為大家分享一篇python調(diào)用其他文件函數(shù)或類的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07對python抓取需要登錄網(wǎng)站數(shù)據(jù)的方法詳解
今天小編就為大家分享一篇對python抓取需要登錄網(wǎng)站數(shù)據(jù)的方法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05