【Python】如何查看内置函数的用法及其源码?

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

目录

1.math模块

2.help函数

3.?的使用

4.Python官网


 

     比如,我们在用math模块,但是不知道这个模块下是否有自己常用的函数,那么如何做呢?

1.math模块

import math
dir(math)

首先,我们导入这个模块,使用dir函数,就可以看到,这个模块下都有哪些函数。

['__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'acos',
 'acosh',
 'asin',
 'asinh',
 'atan',
 'atan2',
 'atanh',
 'ceil',
 'copysign',
 'cos',
 'cosh',
 'degrees',
 'e',
 'erf',
 'erfc',
 'exp',
 'expm1',
 'fabs',
 'factorial',
 'floor',
 'fmod',
 'frexp',
 'fsum',
 'gamma',
 'gcd',
 'hypot',
 'inf',
 'isclose',
 'isfinite',
 'isinf',
 'isnan',
 'ldexp',
 'lgamma',
 'log',
 'log10',
 'log1p',
 'log2',
 'modf',
 'nan',
 'pi',
 'pow',
 'radians',
 'sin',
 'sinh',
 'sqrt',
 'tan',
 'tanh',
 'tau',
 'trunc']

2.help函数

import math
help(math)

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

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

import random
random.__file__

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

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

这里需要注意一下:

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

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

    def expovariate(self, lambd):
        """Exponential distribution.

        lambd is 1.0 divided by the desired mean.  It should be
        nonzero.  (The parameter would be called "lambda", but that is
        a reserved word in Python.)  Returned values range from 0 to
        positive infinity if lambd is positive, and from negative
        infinity to 0 if lambd is negative.

        """
        # lambd: rate lambd = 1/mean
        # ('lambda' is a Python reserved word)

        # we use 1-random() instead of random() to preclude the
        # possibility of taking the log of zero.
        return -_log(1.0 - self.random())/lambd

3.?的使用

如果想大概看一下函数的用法,还可以用下面的语句哦

import math
?math.floor

结果就是:

Docstring:
floor(x)

Return the floor of x as an Integral.
This is the largest integer <= x.
Type:      builtin_function_or_method

这样也可以看到这个函数的用法,是不是比百度也方便的多呢~

4.Python官网

1)首先进入Python官网:https://www.python.org/

2)点击Docs进入:

3)上图中,左边框输入需要查询函数的版本,选择好后,右边框中输入要查找的函数,这里以find为例。

 

4)在该页面找到需要看的函数,这里以str.find为例,点击之后,进入详情,可以看到函数的用法

以上就是几种方法啦~

 

  • 62
    点赞
  • 214
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值