2012年4月5日 星期四

[Python Std Library] Data Types : weakref — Weak references


翻譯自 這裡
Preface :
為了 Performance 考量, 提供此模組讓 garbage collection 可以對 weak references 的 objects 進行回收而減少記憶體的使用量. 另外內建的 list 與 dict 也可以透過繼承來達成 weak reference 的效果 (更多請參考 Weak Reference Support):
  1. class Dict(dict):  
  2.     pass  
  3.   
  4. obj = Dict(red=1, green=2, blue=3)   # this object is weak referenceable  

Changed in version 2.4: Added support for files, sockets, arrays, and patterns.
Changed in version 2.7: Added support for thread.lock, threading.Lock, and code objects.

Note.
Weak references to an object are cleared before the object’s __del__() is called, to ensure that the weak reference callback (if any) finds the object still alive.

接著來看看這個模組上提供的方法 :
- class weakref.ref(object[, callback])
Changed in version 2.4: This is now a subclassable type rather than a factory function; it derives from object.
Return a weak reference to object. The original object can be retrieved by calling the reference object if the referent is still alive; if the referent is no longer alive, calling the reference object will cause None to be returned. If callback is provided and not None, and the returned weakref object is still alive, the callback will be called when the object is about to be finalized; the weak reference object will be passed as the only parameter to the callback; the referent will no longer be available.

It is allowable for many weak references to be constructed for the same object. Callbacks registered for each weak reference will be called from the most recently registered callback to the oldest registered callback.

Exceptions raised by the callback will be noted on the standard error output, but cannot be propagated; they are handled in exactly the same way as exceptions raised from an object’s __del__() method.

Weak references are hashable if the object is hashable. They will maintain their hash value even after the object was deleted. If hash() is called the first time only after the object was deleted, the call will raise TypeError.

Weak references support tests for equality, but not ordering. If the referents are still alive, two references have the same equality relationship as their referents (regardless of the callback). If either referent has been deleted, the references are equal only if the reference objects are the same object.

weakref.proxy(object[, callback])
Return a proxy to object which uses a weak reference. This supports use of the proxy in most contexts instead of requiring the explicit dereferencing used with weak reference objects. The returned object will have a type of either ProxyType or CallableProxyType, depending on whether object is callable. Proxy objects are not hashable regardless of the referent; this avoids a number of problems related to their fundamentally mutable nature, and prevent their use as dictionary keys. callback is the same as the parameter of the same name to the ref() function.

weakref.getweakrefcount(object)
Return the number of weak references and proxies which refer to object.

weakref.getweakrefs(object)
Return a list of all weak reference and proxy objects which refer to object.

- class weakref.WeakKeyDictionary([dict])
Mapping class that references keys weakly. Entries in the dictionary will be discarded when there is no longer a strong reference to the key. This can be used to associate additional data with an object owned by other parts of an application without adding attributes to those objects. This can be especially useful with objects that override attribute accesses.

Note Caution: Because a WeakKeyDictionary is built on top of a Python dictionary, it must not change size when iterating over it. This can be difficult to ensure for aWeakKeyDictionary because actions performed by the program during iteration may cause items in the dictionary to vanish “by magic” (as a side effect of garbage collection).

在 WeakKeyDictionary 上提供下面函數使用 :
WeakKeyDictionary.iterkeyrefs()
New in version 2.5.
Return an iterator that yields the weak references to the keys.

WeakKeyDictionary.keyrefs()
New in version 2.5.
Return a list of weak references to the keys.


- class weakref.WeakValueDictionary([dict])
Mapping class that references values weakly. Entries in the dictionary will be discarded when no strong reference to the value exists any more.

Note Caution: Because a WeakValueDictionary is built on top of a Python dictionary, it must not change size when iterating over it. This can be difficult to ensure for a WeakValueDictionary because actions performed by the program during iteration may cause items in the dictionary to vanish “by magic” (as a side effect of garbage collection).

在 WeakValueDictionary 提供下面函數使用 :
WeakValueDictionary.itervaluerefs()
New in version 2.5.
Return an iterator that yields the weak references to the values.

WeakValueDictionary.valuerefs()
New in version 2.5.
Return a list of weak references to the values.


- class weakref.WeakSet([elements])
New in version 2.7.
Set class that keeps weak references to its elements. An element will be discarded when no strong reference to it exists any more.

Weak Reference Objects :
Weak reference 物件上沒有特殊的函數或屬性, 但你可以透過呼叫它來取得它所參考到的物件 :
>>> import weakref
>>> def calback(o):
... print("\t[Info] Weak ref obj={0}...".format(o))
...
>>> class Obj:
... pass
...
>>> o = Obj()
>>> r = weakref.ref(o, calback)
>>> o2 = r()
>>> o is o2
True

如果所有的參考都失效, 則呼叫 reference object 會返回 None :
>>> del o, o2 # 移除參考 o, o2
[Info] Weak ref obj=... # 當所有參考都失效, calback() 被呼叫
>>> print r()
None

更進階的使用, 你可以繼承 Weak Reference 並重新定義自己的客製化用途. 例如底下的類別 ExtendedRef 會在被呼叫時紀錄總共呼叫次數, 並回傳包含參考物件與該呼叫次數的 tuple :
  1. import weakref  
  2.   
  3. class ExtendedRef(weakref.ref):  
  4.     def __init__(self, ob, callback=None, **annotations):  
  5.         super(ExtendedRef, self).__init__(ob, callback)  
  6.         self.__counter = 0  
  7.         for k, v in annotations.iteritems():  
  8.             setattr(self, k, v)  
  9.   
  10.     def __call__(self):  
  11.         """Return a pair containing the referent and the number of  
  12.         times the reference has been called.  
  13.         """  
  14.         ob = super(ExtendedRef, self).__call__()  
  15.         if ob is not None:  
  16.             self.__counter += 1  
  17.             ob = (ob, self.__counter)  
  18.         return ob  
Example :
在使用 WeakValueDictionary 可以讓你將該物件的 Value 設為 Weak reference, 也就是說當 Value 參考到的物件已經沒有任何參考時, 則該筆 Entry 即被移除 :
>>> o = Obj()
>>> o
<__main__.Obj instance at 0x7f7b7293cd88>
>>> wdict['o'] = o
>>> wdict['o'] # 與參考 o 指向同一物件, 但 wdict['0'] 是弱參考
<__main__.Obj instance at 0x7f7b7293cd88>
>>> _ # 要注意在 Iterative mode 還有一個預設變數 _ 也參考到該物件
<__main__.Obj instance at 0x7f7b7293cd88>
>>> del o # 移除參考 o
>>> 2 # 移除預設變數 _ 參考到物件 <__main__.Obj instance at 0x7f7b7293cd88>
2
>>> _
2
>>> wdict['o'] # 因為物件 <__main__.Obj instance at 0x7f7b7293cd88> 上已經沒有參考, 故 wdict['o'] 的 entry 會被移除.
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.6/weakref.py", line 54, in __getitem__
o = self.data[key]()
KeyError: 'o'

在實際應用上, 可以用 WeakValueDictionary 當作 Object 的 Reference Store, 透過 Object id 來存取物件 :
  1. import weakref  
  2.   
  3. _id2obj_dict = weakref.WeakValueDictionary()  
  4.   
  5. def remember(obj):  
  6.     oid = id(obj)  
  7.     _id2obj_dict[oid] = obj  
  8.     return oid  
  9.   
  10. def id2obj(oid):  
  11.     return _id2obj_dict[oid]  
This message was edited 26 times. Last update was at 06/04/2012 11:48:29

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...