5

と は Python の組み込み数値型 ( 、、、および)reprで同一ですか、それとも 2 つが異なる結果をもたらす可能性がある (難解な?) 状況はありますか?strintboolfloatcomplex

SO に関する関連する質問 ( this one__repr__など) は、実装方法と__str__実装方法が異なり、文字列に対して異なる値を返すことに焦点を当てていますが、数値の実際の実装については何も見つかりません。

4

1 に答える 1

3

Your primary source of information on this is http://hg.python.org/cpython/file/tip/Objects For example, in boolobject.c:

PyTypeObject PyBool_Type = {
    ...stuff...

    bool_repr,                                  /* tp_repr */

    ...stuff...

    bool_repr,                                  /* tp_str */

so yes, they're guaranteed to be the same.

For floats, float_repr is different from float_str and depends on sys.float_repr_style.

于 2012-11-20T10:59:20.640 に答える