利用python 的__dict__,讓全部的對象共享變量。python
改了下,讓全部的對象共享 公共類變量,也能夠將公共類變量設置爲 任務池。對象
class Pub(object):
count=0
state='p_init'it
class Borg(object):
__shard_state={'count':0}
def __init__(self):
self.__dict__['pub']=Pub
self.state='init'
self.pub.count+=1
#print (self.state)
def __str__(self):
return self.stateclass
if __name__=='__main__':
print (Pub.count)
rm1=Borg()
rm2=Borg()
rm1.state='st'
rm2.state='st2'
rm2.pub.state='p2'變量
print ('rm1:%s'%rm1,rm1.pub.state)
print ('rm2:%s'%rm2,rm1.pub.state)
print (Pub.count)object