获取 `object.__doc__` 作为原始字符串
除了在源代码中的文档本身前面添加“r”之外,是否有办法将 object.__doc__
作为原始字符串获取?
我里面有乳胶代码,'\r's、'\f's 等会产生问题。
Is there a way to get object.__doc__
as a raw string, apart from adding an 'r' in-front of the doctring itself in the source code?
I have latex code inside and the '\r's, '\f's etc are creating problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不存在“原始字符串”这样的 Python 类型——存在原始字符串文字,它们只是一种语法方法(众多方法中的一种)指定字符串类型的常量(即文字)。因此,“获取”某些“作为原始字符串”是没有意义的。您可以编写文档字符串作为原始字符串文字(即,使用前缀
r
- 这正是表示原始字符串文字的方式,这是将此类常量标识为Python 编译器),或者将其中的任何反斜杠加倍(指定包括反斜杠字符的常量字符串的另一种方法),但这与以某种方式“获取”它们无关。There's no such Python type as "raw string" -- there are raw string literals, which are just one syntax approach (out of many) to specify constants (i.e., literals) that are of string types. So "getting" something "as a raw string" just makes no sense. You can write docstrings as raw string literals (i.e., with the prefix
r
-- that's exactly what denotes a raw string literal, the specific syntax that identifies such a constant to the Python compiler), or else double up any backslashes in them (an alternative way to specify constant strings including backslash characters), but that has nothing to do with "getting" them one way or another.原始字符串和其他字符串之间的区别只是源代码文字语法的问题。一旦解析,就不存在“原始”字符串对象。 repr(object.__doc__) 的结果始终是这样的,您可以将结果复制并粘贴到 python 源脚本中并获取原始字符串。
考虑:
the difference between a raw string and otherwise is just a matter of source code literal syntax. once parsed, there is no 'raw' string object. the result of
repr(object.__doc__)
will always be such that you can copy and paste the result into a python source script and get the original string.consider:
不,你必须添加 r。如果不添加 r,则无论您做什么都无法确保返回原始字符串。
如果您不喜欢原始字符串,另一种选择是使用额外的反斜杠转义字符串中的反斜杠。
No, you have to add the r. If you don't add the r, there's no way you can be sure to get back the original string no matter what you do.
If you don't like raw strings the other alternative is to escape your backslashes in the strings with an extra backslash.