In Python 2.x, using backticks to get decimal string from int object is Horrible?
Because backticks are repr(), not str()? I have noticed that when I answering this question.
In Python source, they have same function in Python source, intobject.c
(reprfunc)int_to_decimal_string, /* tp_repr */
....
(reprfunc)int_to_decimal_string, /* tp_str */
What do you think?
Well, I wouldn’t say it’s “horrible”, but I feel it isn’t right for at least four reasons:
str(my_number)states your intent more clearly than surroundingmy_numberby backticks. (See “Readability counts” in the Zen of Python).The implementation of Python in C is just one possible implementation; there is Jython, IronPython, PyPy and so on, and unless there is an explicit statement in the Python specification somewhere that
repr()andstr()is the same for integer objects, I wouldn’t to rely on that behaviour.Backticks are gone in Python 3.x.
If your number happens to be so large that it cannot be represented by an
int, Python promotes it automatically to a long integer, and for that,repr()andstr()differs.See this example: