Are repr and str always identical on Pythons builtin numeric types?

六眼飞鱼酱① 提交于 2019-12-10 14:09:53

问题


Are repr and str identical on Pythons built-in numeric types (int, bool, float, and complex), or are there (esoteric?) situations where the two may yield different results?

Related questions on SO (such as this one) focus on how __repr__ and __str__ may be implemented differently, and return different values for strings, but I can't find anything on the actual implementation on numbers.


回答1:


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.



来源:https://stackoverflow.com/questions/13471581/are-repr-and-str-always-identical-on-pythons-builtin-numeric-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!