1

モジュール内のメソッドガウスなどのメソッドがランダムに何をするのかを知りたい場合、Pythonインタープリターを使用してどのように行うのですか?たとえば、Pythonインタープリターのコンソールにimport randomと入力した後、実際のファイルを確認せずに、モジュールランダムでメソッドgaussの実際のコードを見つけるために何ができるでしょうか。前もって感謝します!

4

3 に答える 3

5

試す:

import inspect
inspect.getsource(random.gauss)
于 2012-07-15T05:23:45.297 に答える
3

IPythonを使用している場合は、次のように実行できます。

>>> random.gauss??
于 2012-07-15T05:22:43.300 に答える
2

デフォルトのPythonシェルからは利用できませんが、これはの多くの機能の1つです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によって提案されたタイピングと同じです

于 2012-07-15T05:24:16.880 に答える