标签:
感觉property的用法挺奇特,现在能力有限,几乎没用过,仅记录下
class Demo 和Demo1的最终效果是一样的;Demo2的对象可以将run方法当作变量属性一样调用,即 Demo2().run
class Demo(object): def __init__(self): self._n=None @property def m(self): return self._n @m.setter def m(self,value): self._n=value @m.deleter def m(self): del self._n class Demo1(object): def __init__(self): self._n=None def getn(self): return self._n def setn(self,value): self._n=value def deln(self): del self._n m=property(getn,setn,deln,‘‘) class Demo2(object): @property def run(self): return ‘echo from run ‘
写了个简单的unittest
import unittest,sys sys.path.append(‘..‘) from com.shark import * class Test(unittest.TestCase): def setUp(self): self.d=Demo() def test1(self): self.d.m=1 self.assertEqual(self.d.m,1) #test1 self.d.m=2 self.assertEqual(self.d.m,2) #test2 del self.d.m with self.assertRaises(AttributeError): print self.d.mss def tearDown(self): print ‘done‘ if __name__==‘__main__‘: unittest.main()
标签:
原文地址:http://www.cnblogs.com/Citizen/p/4733688.html