作者KSJ (阿真)
看板Python
标题Re: [问题] 如何找到class的instance??
时间Mon Nov 30 23:15:47 2009
※ 引述《sbrhsieh (偶尔想摆烂一下)》之铭言:
: ※ 引述《KSJ (阿真)》之铭言:
: : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: : 这句话就好像 在dictionary里 有二个一样的key
: : 但里面存着不同的东西
: : 不可思议 但在qgis的python console下真的存在@_@a
: : 我目前的解决辨法是 类似於 前几篇的文章中
: : "把instance放入instance的property中"
: : 不过因为instance抓不到 所以也没辨法抓到instance中的property
: : 所以我就把 "instance放入比它自己还上一层的某个class的property中"
: : (我想我说的class应该也是某个instance吧)
: : 再去看它的 property 就是 我想要的instance了
: : 不过这的确会碰到 所谓的 每个instance都会不能清除的问题
: 如果你的需求是在任意时刻访问一 class/type 的所有 live object,那麽
: globals() 回报的 mapping 里有同名的现象(虽然我不认为会有)并无关紧要,
http://imgur.com/BK9v7.png
这个是qgis下的python console
红线是我的instance跟module同名部份
蓝线是官方的plugin 也跟我的一样有同名的instance跟module
似乎是qgis的python console里才会发生的
不知道是什麽原因@_@
: live object 表示有一个以上的 strong reference 指向它,这些 strong
: reference 在哪并不重要。
: 要达到你的需求,之前版友 Falldog 提供的作法加上 weakref module 的协助
: 就可以实做出一个可接受的方案:
: from weakref import WeakValueDictionary
: class foo(object):
: __live_objects = WeakValueDictionary()
: def __new__(cls, *args):
: obj = super(foo, cls).__new__(cls, *args)
: cls.__live_objects[id(obj)] = obj
: return obj
: def __init__(self, data=None):
: self.data = data
: @staticmethod
: def visit_live_objects(visitor):
: for x in foo.__live_objects.itervalues():
: visitor(x)
为了这个继承於 "object"
我正在努力地看...orz
http://www.cafepy.com/article/python_types_and_objects/
python_types_and_objects.html
(短网址
http://0rz.tw/xy7NW)
不过里面有用到object底下的东西吗@_@? (我在字面上看不出来...)
cls是什麽啊 囧 在我认知里 class下的def 的第一个参数就是他自己(self)
用self来解释似乎也通
关於 @staticmethod 看了一下官方文件
感觉是定义了class下的function
这让我对 class 跟 instance 的适用时机 更混杂了 囧
: # demo code:
: def dump(x):
: print x.data,
: A=map(foo, ('Item %d' % x for x in xrange(10)))
: foo.visit_live_objects(dump)
: # output:
: # Item 7 Item 8 Item 0 Item 1 Item 2 Item 3 Item 4 Item 5 Item 9 Item 6
: del A[5:]
: foo.visit_live_objects(dump)
: # output:
: # Item 3 Item 0 Item 2 Item 4 Item 1
: a = foo('PTT')
: foo.visit_live_objects(dump)
: # output:
: # Item 3 PTT Item 0 Item 2 Item 4 Item 1
: a='ptt'
: foo.visit_live_objects(dump)
: # output:
: # Item 3 Item 0 Item 2 Item 4 Item 1
到这里我去查了weak reference...
稍稍了解他的用法(就像在此处的用法)
不过有没有比较平常的例子??
例如创了一个weak reference的变数,wref
这个变数感觉上用起来很不踏实 囧
因为wref参考的东西随时可能不见
这样的东西怎麽用呢
还是说要创weakreference的变数 必须先有strongreference(sref)才行
不然创了wref没有sref不就可能一创完就被系统删了?@@a
以下这里也还在努力中... 连type都继承了orz...
问不出疑惑或一些小结 因为还末消化.. 囧>
不过还是谢谢s大热心讲解 跟f大的观念提供 <(_ _)>
: 如果考虑到可能数个 class/type 都有此种需求,而且这个需求某种程度上也
: 算是一种通用的 feature,可以考虑写成 meta class 来为多个 class 提供
: cache live object 的支援。
: 大致上如下:
: class metaLiveObjectCache(type):
: def __init__(cls, name, bases, attrs):
: super(metaLiveObjectCache, cls).__init__(cls, name, bases, attrs)
: setattr(cls, '_%s__live_objects' % cls.__name__, WeakValueDictionary())
: # hook __new__
: old_new = cls.__new__
: def proxy_new(klass, *args):
: obj = old_new(klass, *args)
: getattr(cls, '_%s__live_objects' % cls.__name__)[id(obj)] = obj
: return obj
: proxy_new.__name__ = old_new.__name__
: proxy_new.__doc__ = old_new.__doc__
: cls.__new__ = staticmethod(proxy_new)
: # install static method: visit_live_objects
: def visit_live_objects(visitor):
: for x in getattr(cls, '_%s__live_objects' % cls.__name__).itervalues():
: visitor(x)
: cls.visit_live_objects = staticmethod(visit_live_objects)
: 前一例中的 foo class 可以简化如下:
: class bar(object):
: __metaclass__ = metaLiveObjectCache
: def __init__(self, data=None):
: self.data = data
题外话 这里看到了 foo 跟 bar 变数
就去查了一下...
http://www.xoopscube.tw/modules/news/article.php?storyid=143
蛮微妙的 囧>
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.63.180
1F:→ yungyuc:sbrhsieh 教的是万恶的 meta-classing,不好懂是应该的 11/30 23:24
2F:→ sbrhsieh:不会 meta-programming 并不会有什麽坏处吧? 12/01 01:13