新聞中心
在用Python進(jìn)行各種分析的時(shí)候,我們會(huì)用到各種各樣的函數(shù),比如,我們用SQL時(shí),經(jīng)常使用join、max等各種函數(shù),那么想看Python是否有這個(gè)函數(shù),這個(gè)時(shí)候可能大部分人會(huì)百度,那么如何不使用百度,而用Python本身來查找函數(shù),學(xué)習(xí)函數(shù)的用法呢?下面,小白就總結(jié)一下自己一些經(jīng)歷~

創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元天祝藏族自治做網(wǎng)站,已為上家服務(wù),為天祝藏族自治各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
比如,我們?cè)谟胢ath模塊,但是不知道這個(gè)模塊下是否有自己常用的函數(shù),那么如何做呢?
方法一
import math dir(math)
首先,我們導(dǎo)入這個(gè)模塊,使用dir函數(shù),就可以看到,這個(gè)模塊下都有哪些函數(shù)。
['__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']
這種方法是得到一個(gè)函數(shù)列表,當(dāng)然,這里還可以使用help函數(shù):
import math help(math)
如果還是對(duì)函數(shù)不是特別了解,可以到方法的文件中去看函數(shù)的定義,利用***.__file__查看位置,然后打開后綴名為.py的文件。
import random random.__file__
結(jié)果為:這樣就可以到這個(gè)py文件中查看源碼
'D:\\Anaconda2\\envs\\py3\\lib\\random.py'
這里需要注意一下:
***.pyc的文件是編譯后的文件,打開是看不懂的,所以要看***.py文件。
在里面可以搜想看的函數(shù),具體的定義,比如說,搜了expovariate函數(shù),下面把該方法貼出來,這樣就可以看到該方法是如何聲明的辣,這樣是不是也很方便,而且了解的更加透徹呢~
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 文章標(biāo)題:創(chuàng)新互聯(lián)Python教程:一分鐘學(xué)會(huì)如何查看Python內(nèi)置函數(shù)的用法及其源碼
文章路徑:http://fisionsoft.com.cn/article/dpdogog.html


咨詢
建站咨詢
