十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
如果你对Python技巧——代码效率的实际应用不是很了解的话,你就可以浏览以下的文章对Python技巧的实际应用有个更深入的了解,希望你在浏览完以下的文章你能从中学到自己想要的东西。

成都创新互联是一家专业提供巩留企业网站建设,专注与成都网站建设、网站设计、H5开发、小程序制作等业务。10年已为巩留众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
对象相等。您认为type(a) == type(b)和type(a) is type(b)之间的不同是什么?为什么会选择后者?函数isinstance()与这有什么关系?
在例4.1 中我们提供了一段脚本来演示在运行时环境使用isinstance() 和 type()函数。随后我们讨论type()的使用以及怎么将这个例子移植为改用 isinstance()。运行 typechk.py, 我们会得到以下输出:
- -69 is a number of type: int
 - 9999999999999999999999 is a number of type: long
 - 98.6 is a number of type: float
 - (-5.2+1.9j) is a number of type: complex
 - xxx is not a number at all!!
 
例4.1 检查类型(typechk.py)函数displayNumType() 接受一个数值参数,Python技巧中使用内建函数type()来确认数值的类型(或不是一个数值类型)。
- #!/usr/bin/env python
 - def displayNumType(num):
 - print num, 'is',
 - if isinstance(num, (int, long, float, complex)):
 - print 'a number of type:', type(num).__name__
 - else:
 - print 'not a number at all!!'
 - displayNumType(-69)
 - displayNumType(9999999999999999999999L)
 - displayNumType(98.6)
 - displayNumType(-5.2+1.9j)
 - displayNumType('xxx')
 
以上就是对Python技巧——代码效率相关的内容的介绍,望你会有所收获。
【编辑推荐】