Python misc

1. property

class C():
def SetIt(self):
self.a = 1
def GetIt(self):
return self.a
content = property(SetIt, GetIt)

c = C()
c.content = 1
print c.content # will print 1

2. id

id(1) # this is a number..

3. weakref

import weakref
class C():
def func(self):
pass

c = C()
c.func()

p = weakref.proxy(c)
p.func()

r = weakref.ref(c)
r().func()

 

import weakref

_id2obj_dict = weakref.WeakValueDictionary()

def remember(obj):
oid = id(obj)
_id2obj_dict[oid] = obj
return oid

def id2obj(oid):
return _id2obj_dict[oid]

 

 

posted @ 2012-03-22 16:38  大兵八世  阅读(662)  评论(0)    收藏  举报