在看celery的時候,發現裡面有這麼一句
print('Request: {0!r}'.format(self.request))
關於裡面的{0!r}
是什麼意思翻了一下文件。
文件裡是這麼描述的
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
------
Two conversion flags are currently supported: '!s' which calls str() on the value, and '!r' which calls repr().
Some examples:
"Harold's a clever {0!s}" # Calls str() on the argument first
"Bring out the holy {name!r}" # Calls repr() on the argument first
是說感嘆號後面跟的是conversion,而conversion有兩個值.
分別是s對應str()函式, r對應repr()函式。
因此上面的翻譯一下類似於下面
"Harold's a clever {0!s}" == "Harold's a clever str({0})"
"Bring out the holy {name!r}" == "Bring out the holy repr({name})"
實際呼叫時的寫法應該是
"Harold's a clever {0!s}".format(string) == "Harold's a clever {0}".format(str(string))
"Bring out the holy {name!r}".format(string) == "Bring out the holy {name}".format(repr(string))