0

Squish フレームワークを使用してアプリケーションの自動テストを作成しています。テスト スクリプトには、次のコードを呼び出すコードがありますrandrange

a = 5.0
random.randrange( int(a) )

この呼び出しの結果、オンラインlib/python2.6/random.py:171で非常に奇妙なエラーが発生します。

TypeError: int() argument must be a string or a number, not 'int'

random.py のコンテキストの 171 行目は、randrange関数のコードの最初の行です。

def randrange(self, start, stop=None, step=1, int=int, default=None,
              maxwidth=1L<<BPF):
    """Choose a random item from range(start, stop[, step]).

    This fixes the problem with randint() which includes the
    endpoint; in Python this is usually not what you want.
    Do not supply the 'int', 'default', and 'maxwidth' arguments.
    """

    # This code is a bit messy to make it fast for the
    # common case while still doing adequate error checking.
    istart = int(start)    # <---this is line 171
    if istart != start:
        raise ValueError, "non-integer arg 1 for randrange()"
    if stop is default:
        ...

もちろん、デバッガーコンソールで確認しましたが、実際のタイプは次のintとおりです。

>>> __builtin__.type(start)
<type 'int'>
>>> 

しばらくグーグルで調べた後、Squish API ドキュメントで答えを得ました。

Python プログラマーは、次のような整数型変換が機能しないことに注意する必要がありint(x)ます。x = cast(x, int)またはx = cast(x, "int") 代わりに使用します。または、必要に応じて、 を実行してimport __builtin__から を使用しますx = __builtin__.int(x)。(これは、Squish が Python で独自のintオブジェクトを実装するために必要です。)

じゃあ良いよ。しかし、私の質問は次のとおりです。名前の競合がある場合、Python オブジェクト タイプをチェックインする方法は? <type 'something'>が宣言された場所を確認するにはどうすればよいですか?

4

1 に答える 1