当前位置:Gxlcms > Python > 如何查看python内置函数源码

如何查看python内置函数源码

时间:2021-07-01 10:21:17 帮助过:91人阅读

在用Python进行各种分析的时候,我们会用到各种各样的函数,比如,我们用SQL时,经常使用join、max等各种函数,那么想看Python是否有这个函数,这个时候可能大部分人会百度,那么如何不使用百度,而用Python本身来查找函数,学习函数的用法呢?

这里还可以使用help函数:(推荐学习:Python视频教程)

  1. import math
  2. help(math)

help函数会得到一个带有说明的函数列表,如下:

python-12.png

如果还是对函数不是特别了解,可以到方法的文件中去看函数的定义,利用***.__file__查看位置,然后打开后缀名为.py的文件。

  1. import random
  2. random.__file__

结果为:这样就可以到这个py文件中查看源码

  1. 'D:\\Anaconda2\\envs\\py3\\lib\\random.py'

这里需要注意一下:

***.pyc的文件是编译后的文件,打开是看不懂的,所以要看***.py文件。

在里面可以搜想看的函数,具体的定义,比如说,我搜了expovariate函数,下面把该方法贴出来,这样就可以看到该方法是如何声明的辣,这样是不是也很方便,而且了解的更加透彻呢~

  1. def expovariate(self, lambd):
  2. """Exponential distribution.
  3. lambd is 1.0 divided by the desired mean. It should be
  4. nonzero. (The parameter would be called "lambda", but that is
  5. a reserved word in Python.) Returned values range from 0 to
  6. positive infinity if lambd is positive, and from negative
  7. infinity to 0 if lambd is negative.
  8. """
  9. # lambd: rate lambd = 1/mean
  10. # ('lambda' is a Python reserved word)
  11. # we use 1-random() instead of random() to preclude the
  12. # possibility of taking the log of zero.
  13. return -_log(1.0 - self.random())/lambd

更多Python相关技术文章,请访问Python教程栏目进行学习!

以上就是如何查看python内置函数源码的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行