python动态类型判断是否合法

来源:互联网 发布:淘宝外卖粮票怎么获得 编辑:程序博客网 时间:2024/06/05 17:46

python是一种动态类型的语言。一个属性,既可以给赋值一个数字,也可以给赋值一个字符串。那么,在项目开发中,怎么才能做到检测赋值的时候输入数据的合法性呢?

----------------------------------

先看下面的代码

[plain] view plain copy
  1. # -*- coding:utf-8 -*-  
  2. '''  
  3. Created on 2013-3-29  
  4.   
  5. @author: naughty  
  6. '''  
  7.   
  8. class A(object):  
  9.     def __init__(self):  
  10.         self.__age=10  
  11. a=A()  
  12. print a.__age  

初始化了一个A类的对象a,然后打印__age这个属性,结果如下,提示不存在。

[plain] view plain copy
  1. Traceback (most recent call last):  
  2.   File "H:\final\code\PyTestService\comz\test\Copy of Copy of Test.py", line 12, in <module>  
  3.     print a.__age  
  4. AttributeError: 'A' object has no attribute '__age'  

python中__attr,以__开头的属性是private的。外界访问不到。

---------------------------------------

有了上面的这点知识,那么怎么做到我们题目中说的,输入数据的类型检查呢。

这就使用到了property和setter、deleter的知识了。

先看下property属性

[python] view plain copy
  1. ''''' 
  2. Created on 2013-3-29 
  3.  
  4. @author: naughty 
  5. '''  
  6.   
  7. class A(object):  
  8.       
  9.     def __init__(self, a):  
  10.         self.__x = a  
  11.      
  12.     @property  
  13.     def x(self):  
  14.         assert False,"hi one"  
  15.         #=======================================================================  
  16.         # return self.__x  
  17.         #=======================================================================  
  18.      
  19. a = A(30)  
  20. print a.x  

上面代码的执行结果如下:

[python] view plain copy
  1. Traceback (most recent call last):  
  2.   File "H:\final\code\PyTestService\comz\test\Copy of Test.py", line 21in <module>  
  3.     print a.x  
  4.   File "H:\final\code\PyTestService\comz\test\Copy of Test.py", line 15in x  
  5.     assert False,"hi one"  
  6. AssertionError: hi one  

可以看出来,当给了property注解的时候,访问这个属性,就会去访问注解所在的函数。此时,我们就可以在函数中做一些自己想要的工作了。

再看setter

[python] view plain copy
  1. ''''' 
  2. Created on 2013-3-29 
  3.  
  4. @author: naughty 
  5. '''  
  6.   
  7. class A(object):  
  8.       
  9.     def __init__(self, a):  
  10.         self.__x = a  
  11.      
  12.     @property  
  13.     def x(self):  
  14.         return self.__x  
  15.      
  16.     @x.setter  
  17.     def x(self, value):  
  18.         if isinstance(value, str):  
  19.             assert False"hi two"  
  20.   
  21. a = A(30)  
  22. a.x="string"  

上面代码的运行结果如下:

[plain] view plain copy
  1. Traceback (most recent call last):  
  2.   File "H:\final\code\PyTestService\comz\test\Copy of Test.py", line 23, in <module>  
  3.     a.x="string"  
  4.   File "H:\final\code\PyTestService\comz\test\Copy of Test.py", line 20, in x  
  5.     assert False, "hi two"  
  6. AssertionError: hi two  

当我们给a.x赋值的时候,调用了x.setter注解所在的函数。此时,如果我们赋值的是一个字符串,那么会抛出一个错误。在这个setter所在的函数里面,就可以做到我们题目中所描述的类型检查了。

再看deleter

[python] view plain copy
  1. # -*- coding:utf-8 -*-  
  2. ''''' 
  3. Created on 2013-3-29 
  4.  
  5. @author: naughty 
  6. '''  
  7.   
  8. class A(object):  
  9.       
  10.     def __init__(self, a):  
  11.         self.__x = a  
  12.      
  13.     @property  
  14.     def x(self):  
  15.         return self.__x  
  16.      
  17.     @x.setter  
  18.     def x(self, value):  
  19.         if isinstance(value, str):  
  20.             assert False"hi two"  
  21.  
  22.     @x.deleter  
  23.     def x(self):  
  24.         assert False,"hi,three"  
  25.       
  26. a = A(30)  
  27. a.x = 3  
  28. del a.x  

我们在删除的时候会执行deleter注解坐在的函数。这里,我们没有在deleter中删除x属性,而是抛出了一个错误。

--------------------------------------------------------------------------

上面就是对输入数据进行类型检查了。比较繁琐。

下一篇文章介绍了“数据描述符”,也能够做到检查数据输入是否合法。

0 0
原创粉丝点击