Python __setattr__、 __getattr__、 __delattr__、__call__用法示例
getattr
`getattr`函數(shù)屬于內(nèi)建函數(shù),可以通過函數(shù)名稱獲取
value = obj.attribute
value = getattr(obj, "attribute")
使用`getattr`來實現(xiàn)工廠模式
#一個模塊支持html、text、xml等格式的打印,根據(jù)傳入的formate參數(shù)的不同,調(diào)用不同的函數(shù)實現(xiàn)幾種格式的輸出
import statsout
def output(data, format="text"):
output_function = getattr(statsout, "output_%s" %format)
return output_function(data)
__call__
`__call__`方法用于實例自身的調(diào)用:
class storage(dict):
# __call__方法用于實例自身的調(diào)用
#達(dá)到()調(diào)用的效果
def __call__ (self, key):
try:
return self[key]
except KeyError, k:
return None
s = storage()
s['key'] = 'value'
print s(key) #調(diào)用__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__`函數(shù)是用來設(shè)置對象的屬性,通過object中的__setattr__函數(shù)來設(shè)置屬性:
class A(object):
def __setattr__(self, *args, **kwargs):
print 'call func set attr'
return object.__setattr__(self, *args, **kwargs)
__delattr__
`__delattr__`函數(shù)式用來刪除對象的屬性:
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__函數(shù)實現(xiàn)_Executable函數(shù)對象為可調(diào)用的
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對象,調(diào)用__init__函數(shù)
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)
因此,加入我們初始化對象,并調(diào)用某函數(shù)如下:
client = APIClient(...)
#會調(diào)用__getattr__函數(shù),從而調(diào)用__call__函數(shù)
client.something.get()
相關(guān)文章
Python ArgumentParse的subparser用法說明
這篇文章主要介紹了Python ArgumentParse的subparser用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python numpy有哪些常用數(shù)據(jù)類型
Numpy提供了兩種基本的對象:ndarray(N-dimensional Array Object)和 ufunc(Universal Function Object)。ndarray是存儲單一數(shù)據(jù)類型的多維數(shù)組,而ufunc則是能夠?qū)?shù)組進(jìn)行處理的函數(shù)2023-02-02keras-siamese用自己的數(shù)據(jù)集實現(xiàn)詳解
這篇文章主要介紹了keras-siamese用自己的數(shù)據(jù)集實現(xiàn)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06