温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

查看Python内置函数的方法有哪些

发布时间:2020-08-13 13:55:25 来源:亿速云 阅读:209 作者:小新 栏目:编程语言

小编给大家分享一下查看Python内置函数的方法有哪些,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

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

比如,我们在用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']

这种方法是得到一个函数列表,当然,这里还可以使用help函数:

import math
help(math)

查看Python内置函数的方法有哪些

如果还是对函数不是特别了解,可以到方法的文件中去看函数的定义,利用***.__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

看完了这篇文章,相信你对查看Python内置函数的方法有哪些有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI