135-1821-9792

Python中提高代码效率技巧是怎么样的

这篇文章将为大家详细讲解有关Python中提高代码效率技巧是怎么样的,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

成都创新互联主营开化网站建设的网络公司,主营网站建设方案,App定制开发,开化h5成都小程序开发搭建,开化网站营销推广欢迎开化等地区企业咨询

对象相等。您认为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中提高代码效率技巧是怎么样的就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站名称:Python中提高代码效率技巧是怎么样的
转载来于:http://kswsj.com/article/pggeps.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号