如果我想要查看一個方法的作用,例如隨機模塊中的gauss方法,我將如何使用Python解釋器進行操作?例如,我輸入隨機導入Python解釋器的控制檯後,我可以做什麼來隨機地在模塊中找到方法gauss的實際代碼,而無需查看實際文件。提前致謝!閱讀庫方法定義
Q
閱讀庫方法定義
1
A
回答
5
嘗試:
import inspect
inspect.getsource(random.gauss)
3
如果您使用IPython,你可以這樣做:
>>> random.gauss??
2
雖然不能從默認的Python殼,這是衆多功能之一ipython
。
In [4]: %psource random.gauss
def gauss(self, mu, sigma):
"""Gaussian distribution.
mu is the mean, and sigma is the standard deviation. This is
slightly faster than the normalvariate() function.
Not thread-safe without a lock around calls.
"""
# When x and y are two variables from [0, 1), uniformly
# distributed, then
#
# cos(2*pi*x)*sqrt(-2*log(1-y))
# sin(2*pi*x)*sqrt(-2*log(1-y))
#
# are two *independent* variables with normal distribution
# (mu = 0, sigma = 1).
# (Lambert Meertens)
# (corrected version; bug discovered by Mike Miller, fixed by LM)
# Multithreading note: When two threads call this function
# simultaneously, it is possible that they will receive the
# same return value. The window is very small though. To
# avoid this, you have to use a lock around all calls. (I
# didn't want to slow this down in the serial case by using a
# lock here.)
random = self.random
z = self.gauss_next
self.gauss_next = None
if z is None:
x2pi = random() * TWOPI
g2rad = _sqrt(-2.0 * _log(1.0 - random()))
z = _cos(x2pi) * g2rad
self.gauss_next = _sin(x2pi) * g2rad
return mu + z*sigma
這是一樣的打字random.gauss??
通過@icktoofay
相關問題
- 1. 閱讀方法
- 2. 嚴格的地圖與自定義閱讀方法或類型與自定義閱讀實例
- 3. 閱讀使用string.split方法
- 4. 閱讀像素值方法?
- 5. 閱讀不同方法
- 6. Exif閱讀庫
- 7. 閱讀Java庫
- 8. 無法覆蓋閱讀方法URI :: HTTP
- 9. 使用linq2xml閱讀自定義標籤
- 10. 閱讀servlets中的自定義標題
- 11. 閱讀自定義XML文件
- 12. 如何閱讀spring beans定義
- 13. 閱讀C中的函數定義
- 14. 閱讀時鎖定數據庫[Android] [SQLite]
- 15. 綁定到Objective C庫(VFR閱讀器)
- 16. Azure移動服務Javascript庫和更新 - 沒有方法閱讀
- 17. ITEXT PDF閱讀器無法閱讀PDF
- 18. jsoncpp - 無法定義的Json ::閱讀器由於不完全型
- 19. 無法讀取屬性'取消訂閱'未定義的Angular4
- 20. 風格的FileInputStream的閱讀方法
- 21. 如何閱讀方法調用
- 22. DirectShow的閱讀包含回調方法
- 23. Android線程XML閱讀器方法
- 24. 閱讀lnk文件的一般方法
- 25. 閱讀JSON元素的Java方法
- 26. 閱讀方法跳過節點
- 27. 我如何閱讀Objective C方法
- 28. 閱讀配置節的通用方法
- 29. 如何在PHP中閱讀此方法?
- 30. 閱讀文本文件的方法
的建議爲何不乾脆:'inspect.getsource(random.gauss)' – jamylak 2012-07-15 05:29:55
@jamylak:哦,是的!謝謝 :) – SuperSaiyan 2012-07-15 05:31:42