In python the %r format character seems to able to format anything so why use %s (for strings) or %d for integers. Is there any case in which it is not appropriate to use %r?
Asked
Active
Viewed 79 times
1
-
[relavent](http://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python), This was answered previously. – maximx1 Jan 29 '14 at 15:36
1 Answers
0
%r returns repr() on object, see http://docs.python.org/2/library/functions.html#func-repr, which is basically a method that attempts to convert the object into executable python syntax which will result in the same value as the given param.
This may cause you problems if you want to represent certain objects as strings. (e.g. datetime example - When to use %r instead of %s in Python?)
Basically if you're using it for actual string, it shouldn't matter, for integers it should return the same value if the integer is legal, but (!) if you accidentally pass a different object rather than an integer where you intended to pass an integer, %d will cause issues while %r won't

Community
- 1
- 1

Dror Aharon
- 143
- 1
- 1
- 6