类的多个实体共享某个参数

Python 中类的多个实体对象需要共享某个参数:

>>> class S:
	sd = {'t': 1}

	
>>> 
>>> class T:
	@property
	def t(self):
		return S.sd['t']
	@t.setter
	def t(self, val):
		S.sd['t'] = val

		
>>> t1 = T()
>>> t2 = T()
>>> t1.t
1
>>> t2.t
1
>>> t1.t = 10
>>> t1.t
10
>>> t2.t
10
>>> 

  

 

 

展开阅读全文