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

創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目網(wǎng)站設計制作、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元天祝藏族自治做網(wǎng)站,已為上家服務,為天祝藏族自治各地企業(yè)和個人服務,聯(lián)系電話:028-86922220
比如,我們在用math模塊,但是不知道這個模塊下是否有自己常用的函數(shù),那么如何做呢?
方法一
import math dir(math)
首先,我們導入這個模塊,使用dir函數(shù),就可以看到,這個模塊下都有哪些函數(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']
這種方法是得到一個函數(shù)列表,當然,這里還可以使用help函數(shù):
import math help(math)
如果還是對函數(shù)不是特別了解,可以到方法的文件中去看函數(shù)的定義,利用***.__file__查看位置,然后打開后綴名為.py的文件。
import random random.__file__
結果為:這樣就可以到這個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 本文題目:創(chuàng)新互聯(lián)Python教程:一分鐘學會如何查看Python內置函數(shù)的用法及其源碼
新聞來源:http://www.dlmjj.cn/article/dpdogog.html


咨詢
建站咨詢
