Python 板


LINE

※ 引述《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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP