亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python __setattr__、 __getattr__

系統 1755 0

getattr

`getattr`函數屬于內建函數,可以通過函數名稱獲取

復制代碼 代碼如下:

value = obj.attribute
value = getattr(obj, "attribute")

使用`getattr`來實現工廠模式
復制代碼 代碼如下:

#一個模塊支持html、text、xml等格式的打印,根據傳入的formate參數的不同,調用不同的函數實現幾種格式的輸出

import statsout

def output(data, format="text"):??????????????????????????
??? output_function = getattr(statsout, "output_%s" %format)
??? return output_function(data)

__call__

`__call__`方法用于實例自身的調用:

復制代碼 代碼如下:

class storage(dict):
??? # __call__方法用于實例自身的調用
??? #達到()調用的效果
??? def __call__ (self, key):
???????? try:
???????????? return self[key]
???????? except KeyError, k:
???????????? return None

s = storage()
s['key'] = 'value'
print s(key) #調用__call__

__getattr__

從對象中讀取某個屬性時,首先需要從self.__dicts__中搜索該屬性,再從__getattr__中查找。

復制代碼 代碼如下:

class A(object):?
??? def __init__(self):?
??????? self.name = 'from __dicts__: zdy'?
?
??? def __getattr__(self, item):?
??????? if item == 'name':?
??????????? return 'from __getattr__: zdy'?
??????? elif item == 'age':?
??????????? return 26?
?
a = A()?
print a.name # 從__dict__里獲得的?
print a.age # 從__getattr__獲得的

__setattr__

`__setattr__`函數是用來設置對象的屬性,通過object中的__setattr__函數來設置屬性:

復制代碼 代碼如下:

class A(object):
??? def __setattr__(self, *args, **kwargs):?
??????? print 'call func set attr'?
??????? return object.__setattr__(self, *args, **kwargs)

__delattr__

`__delattr__`函數式用來刪除對象的屬性:

復制代碼 代碼如下:

class A(object):
??? def __delattr__(self, *args, **kwargs):?
??????? print 'call func del attr'?
??????? return object.__delattr__(self, *args, **kwargs)?

例子

完整例子可以參考微博API:http://github.liaoxuefeng.com/sinaweibopy/

復制代碼 代碼如下:

class _Executable(object):

??? def __init__(self, client, method, path):
??????? self._client = client
??????? self._method = method
??????? self._path = path
??? #__call__函數實現_Executable函數對象為可調用的
??? def __call__(self, **kw):
??????? method = _METHOD_MAP[self._method]
??????? if method==_HTTP_POST and 'pic' in kw:
??????????? method = _HTTP_UPLOAD
??????? return _http_call('%s%s.json' % (self._client.api_url, self._path), method, self._client.access_token, **kw)

??? def __str__(self):
??????? return '_Executable (%s %s)' % (self._method, self._path)

??? __repr__ = __str__

class _Callable(object):

??? def __init__(self, client, name):
??????? self._client = client
??????? self._name = name

??? def __getattr__(self, attr):
??????? if attr=='get':
       #初始化_Executable對象,調用__init__函數
??????????? return _Executable(self._client, 'GET', self._name)
??????? if attr=='post':
??????????? return _Executable(self._client, 'POST', self._name)
??????? name = '%s/%s' % (self._name, attr)
??????? return _Callable(self._client, name)

??? def __str__(self):
??????? return '_Callable (%s)' % self._name

??? __repr__ = __str__

而在源碼中,存在下面代碼片段:

復制代碼 代碼如下:

class APIClient(object):
??? '''
??? API client using synchronized invocation.
??? '''
??? ...

??? def __getattr__(self, attr):
??????? if '__' in attr:
??????????? return getattr(self.get, attr)
??????? return _Callable(self, attr)

因此,加入我們初始化對象,并調用某函數如下:

復制代碼 代碼如下:

client = APIClient(...)
#會調用__getattr__函數,從而調用__call__函數
client.something.get()


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产欧美视频一区二区三区 | 欧美开嫩苞实拍视频在线观看 | 久久夜色精品国产噜噜小说 | 亚洲国产精品免费在线观看 | 国产亚洲精品激情一区二区三区 | 奇米影视888狠狠狠777九色 | 久久久香蕉视频 | 日本久久久久亚洲中字幕 | 在线观看国产区 | 亚洲国产精品网站久久 | 国内精品久久久久鸭 | 伊人三区 | 在线影院福利 | 天天干天天添 | 91精品国产免费久久久久久青草 | 91久久国产 | 亚洲va精品中文字幕动漫 | 久久久在线视频精品免费观看 | 高清免费毛片 | 亚洲精品乱码一区二区在线观看 | 99热都是精品 | 四虎国产精品永久在线网址 | 日本不卡在线观看 | 久久精品中文字幕有码日本 | 9191在线精品播放 | 亚洲毛片在线观看 | 久热精品男人的天堂在线视频 | 四虎影院永久 | 久久成人精品视频 | 中文字幕日本在线 | 国产激情 | 狠狠色噜噜狠狠狠8888米奇 | 日日操天天操 | 中文字幕一区在线 | 国产精品热久久毛片 | 午夜国产在线观看 | 91精品国产免费久久 | 婷婷六月激情在线综合激情 | 欧美综合视频在线 | 欧美真人视频一级毛片 | 久久99免费视频 |